There are four collection data types in the Python programming language:
When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.
A list is a collection which is ordered and changeable. In Python lists are written with square brackets.
1. Creating Lists
Lists can be created in two ways
- Using the Constructor of the List Class
- Without using the Constructore of the List Class
1.1 Using the Constructor of the List Class
#Creating an empty List
a=list()
# Create a list with integer elements
b=list([10,20,30,40,50])
# Create a list with string elements
c=list(["Tanaji","Manaji","Shivaji"])
#Create a list with mixed type
d=list([10, 5.2, "Apple","Mango"])
#Create a list using inbuilt range() function
f=list(range(0,5))
1.2 Without using the Constructore of the List Class
#Create a list with integer elements
a=[10,20,30,40,50]
#Create a list with String elements
b=["Tanaji","Manaji","Shivaji"]
#Create a list with Mixed type elements
c=[10, 5.2, "Apple", "Mango"]
1.3 Accepting list elements from user and Displaying it
list=[]
n=int(input("Enter List Size : "))
for i in range(n):
x=int(input())
list.append(x)
print(list)
#Output
Enter List Size : 4
10
20
30
40
[10, 20, 30, 40]
2. Accessing Elements of a List
You access the list items by referring to the index number:
a=[10,20,30,40,50]
print(a[1])
print(a[3])
#Output
20
40
3. Negative Index
Negative indexing means beginning from the end, -1
refers to the last item, -2
refers to the second last item etc.
a=[10,20,30,40,50]
print(a[-1])
print(a[-2])
print(a[-5])
#Output
50
40
10
4. Lists are Mutable
Lists are mutable. The value of any element inside the list can be change at any point of time. The elements of the lists are accessible with their index value. The index value always starts with 0 and ends with (n-1).
a=[10,20,30,40,50]
print(a)
a[4]=60
print(a)
#output
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 60]
Note that the value of 5th element is change to 60.
Indices in the list work in the same way as in string
- Any integer expression can be used as an index number
- If any element that does not exists i the list is accessed, there will be an IndexError
- If the indices are given in negative, then counting happens from the end of the list (Backward).
5. Traversing a List
Lists can be traversed using for and while loop.
First we will see, the traversing of a list using while loop.
a=[10,20,30,40,50]
i=0
while(i<len(a)):
print(a[i])
i=i+1
#Output
10
20
30
40
50
Now we can traverse the list using for loop. First we will see the default method using for loop
a=[10,20,30,40,50]
for i in a:
print(i)
#Output
10
20
30
40
50
Now we will see using range function
a=[10,20,30,40,50]
for i in range(0,len(a)):
print(a[i])
#Output
10
20
30
40
50
6. Deleting Elements from List
Python provides many ways in which the elements in the list can be deleted.
a) pop( ) Operator
If we know the index of the element that we want to delete, then we can use the pop operator
a=[10,20,30,40,50]
x=a.pop(2)
print(a)
#Output
[10, 20, 40, 50]
The pop operator deletes the elements on the provided index and store that element in a variable for further use.
Also if we want to delete last element, use onle pop(). It deletes last added element by default.
a=[10,20,30,40,50]
x=a.pop()
print(a)
#Output
[10, 20, 30, 40]
b) del Operator
The del operator deletes the value on the provided index but it does not store the value for further use.
a=[10,20,30,40,50]
del a[1]
print(a)
#Output
[10, 30, 40, 50]
In order to remove more than one value from a list, del operator with slicing is used.
a=[10,20,30,40,50,60,70,80]
del a[1:3]
print(a)
#Output
[10, 40, 50, 60, 70, 80]
c) remove Operator
We use the remove operator if we know the item that we want to remove of delete from the list(nut not the index).
a=[10,20,30,40,50]
a.remove(30)
print(a)
#Output
[10, 20, 40, 50]