By: Archana Shukla and Rajesh Shukla
List
A list is a collection or group of values of any datatype separated by commas, which is ordered and changeable. In Python lists are written with square brackets. Unlike strings lists are mutable.
Creating a List
>>>Mylist = [“Rose”, “lily”, “Jasmine”]
>>>print(Mylist)
[‘Rose’, ‘lily’, ‘Jasmine’]
Creating empty list
The empty list is []. We can also create an empty list as Mylist=list()
Creating list from Existing sequences
We can also create a list from sequences by using a built-in list type object :
Syntax: L=list(sequence)
Example : creating list from a string
>>>L=list(“good”)
>>> L
[‘g’,’o’,’o’,’d’]
Example : creating list from a tuple:
>>> T=(‘a’,’b’,’c’,’d’,’e’)
>>> L1=list(T)
>>> L1
[‘a’,’b’,’c’,’d’,’e’]
Creating list from keyboard input:
>>>l1=list(input(“enter the list elements:”))
Enter the list elements:2,3,4,5
>>>l1
[2,3,4,5]
List indexing
On the basis of the index values ,we can access the list items.
Index value starts from 0 for the first element in the list and so-on.
Example
Print the second item of the list:
>>>Mylist = [“Rose”, “lily”, “Jasmine”]
>>>print(Mylist[1])
lily
Negative Indexing
Negative indexing means beginning from the end, -1 refers to the last item,-2 refers to the second last item etc.
Example
Print the last item of the list:
>>>Mylist = [“Rose”, “lily”, “Jasmine”]
>>>print(Mylist[-1])
Jasmine
List slicing
We can specify a range of indexes by specifying where to start and where to end the range to slice a list. A slice of list is the subset of the list. When specifying a range, the return value will be a new list with the specified items.
Positive index
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
20 |
30 |
40 |
50 |
60 |
70 |
80 |
90 |
100 |
-10 |
-9 |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Negative index
Example 1
>>>L1= [10,20,30,40,50,60,70,80,90,100]
>>>L1[5:]
[60,70,80,90,100]
>>>L1[2:6]
[30,40,50,60]
>>>L1[-9:-5]
[20,30,40,50]
>>>L1[: : -1]
[100,90,80,70,60,50,40,30,20,10]
Example 2
my_list = [‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(my_list[2:5])
# elements 2 to 4th
[‘m’, ‘p’, ‘u’]
print(my_list[:-5])
# elements beginning to 4th
[‘c’, ‘o’, ‘m’]
print(my_list[5:])
# elements 6th to end
[‘t’, ‘e’, ‘r’]
print(my_list[:])
# elements beginning to end
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
To change or add elements to a list
Lists are mutable, meaning, their elements can be changed unlike string or tuple.We can use assignment operator (=) to change an item or a range of items.
Positive index from left to right (starts from 0)
Example
n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[0]=10
print(n)
output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example
n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[2]=300
n[6]=200
n[8]=80
print(n)
output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 300, 4, 5, 6, 200, 8, 80, 10]
Group value changes:
Example 1:
n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[1:4]=[10,20,30]
#position changed will be 1,2,3 (start to pos-1)
print(n)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 10, 20, 30, 5, 6, 7, 8, 9, 10]
Example 2:
n=[1,2,3,4,5,6,7,8,9,10]
print(n)
n[5:7]=[100,200,300,400,500]
print(n)
# Note: 2 locations are replaced by 5 values
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 100, 200, 300, 400, 500, 8, 9, 10]