To get information
len()
- Returns the number of elements present in the list.
Example
Python
n=[10,20,30,40]
print(len(n))
Output
PowerShell
4
count()
- It returns the number of occurrences of specified item in the list.
Example
Python
n=[1,2,2,2,2,3,3]
print(n.count(1))
print(n.count(2))
print(n.count(3))
print(n.count(4))
Output
PowerShell
1
4
2
0
index()
- Returns the index of first occurrence of the specified item.
Example
Python
n=[1,2,2,2,2,3,3]
print(n.index(1))
print(n.index(2))
print(n.index(3))
print(n.index(4))
Output
PowerShell
0
1
5
ValueError: 4 is not in list
Note
- If the specified element not present in the list then we will get ValueError. Hence before index() method we have to check whether item present in the list or not by using in operator.
To manipulate elements
append()
- We can use append() function to add item at the end of the list.
Example
Python
list=[]
list.append("A")
list.append("B")
list.append("C")
print(list)
Output
PowerShell
['A', 'B', 'C']
Question
Add all elements to list unto 100 which are divisible by 10.
Python
list=[]
for i in range(101):
if i%10==0:
list.append(i)
print(list)
Output
Python
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
insert()
- To insert item at specified index position.
Example
Python
n=[1,2,3,4,5]
n.insert(1,888)
print(n)
Output
PowerShell
[1, 888, 2, 3, 4, 5]
- If the specified index is greater than max index then element will be inserted at last position. If the specified index is smaller than min index then element will be inserted at first position.
Example
Python
n=[1,2,3,4,5]
n.insert(10,777)
n.insert(-10,999)
print(n)
Output
PowerShell
[999, 1, 2, 3, 4, 5, 777]
extend()
- To add all items of one list to another list.
Example
Python
order1=["Chicken","Mutton","Fish"]
order2=["RC","KF","FO"]
order1.extend(order2)
print(order1)
Output
PowerShell
['Chicken', 'Mutton', 'Fish', 'RC', 'KF', 'FO']
Example
Python
order=["Chicken","Mutton","Fish"]
order.extend("Mushroom")
print(order)
Output
PowerShell
['Chicken', 'Mutton', 'Fish', 'M', 'u', 's', 'h', 'r', 'o', 'o', 'm']
remove()
- We can use this function to remove specified item from the list. If the item present multiple times then only first occurrence will be removed.
Example
Python
n=[10,20,10,30]
n.remove(10)
print(n)
Output
PowerShell
[20, 10, 30]
- If the specified item not present in list then we will get ValueError
Python
n=[10,20,10,30]
n.remove(40)
print(n)
Output
PowerShell
ValueError: list.remove(x): x not in list
Note
- Before using remove() method first we have to check specified element present in the list or not by using in operator.
pop()
- It removes and returns the last element of the list.
- This is only function which manipulates list and returns some element.
Example
Python
n=[10,20,30,40]
print(n.pop())
print(n.pop())
print(n)
Output
PowerShell
40
30
[10, 20]
- If the list is empty then pop() function raises IndexError
Example
Python
n=[]
print(n.pop())
Output
PowerShell
IndexError: pop from empty list
Note
- In general we can use append() and pop() functions to implement stack data structure by using list, which follows LIFO(Last In First Out) order.
- In general we can use pop() function to remove last element of the list. But we can use to remove elements based on index.
Example
Python
n=[10,20,30,40,50,60]
print(n.pop())
print(n.pop(1))
print(n.pop(10))
Output
PowerShell
60
20
IndexError: pop index out of range
Note
- List objects are dynamic i.e. based on our requirement we can increase and decrease the size.
clear()
- We can use clear() function to remove all elements of List.
Example
Python
n=[10,20,30,40]
print(n)
n.clear()
print(n)
Output
PowerShell
[10, 20, 30, 40]
[]
To ordering elements
reverse()
- We can use to reverse() order of elements of list.
Example
Python
n=[10,20,30,40]
n.reverse()
print(n)
Output
PowerShell
[40, 30, 20, 10]
sort()
- In list by default insertion order is preserved. If want to sort the elements of list according to default natural sorting order then we should go for sort() method.
- Default natural sorting order is ascending order.
Example
Python
n=[20,5,15,10,0]
n.sort()
print(n)
s=["Dog","Banana","Cat","Apple"]
s.sort()
print(s)
Output
Python
[0,5,10,15,20]
['Apple','Banana','Cat','Dog']
Note
- To use sort() function, compulsory list should contain only homogeneous elements. otherwise we will get TypeError.
Example
Python
n=[20,10,"A","B"]
n.sort()
print(n)
Output
PowerShell
TypeError: '<' not supported between instances of 'str' and 'int'
Remark
- We can sort according to reverse of default natural sorting order by using reverse=True argument.
Example
Python
n=[40,10,30,20]
n.sort()
print(n)
n.sort(reverse=True)
print(n)
n.sort(reverse=False)
print(n)
Output
PowerShell
[10,20,30,40]
[40,30,20,10]
[10,20,30,40]
Aliasing of List
- The process of giving another reference variable to the existing list is called aliasing.
Example
Python
x=[10,20,30,40]
y=x
print(id(x))
print(id(y))
Output
PowerShell
1234545678
1234545678
- The problem in this approach is by using one reference variable if we are changing content, then those changes will be reflected to the other reference variable.
Python
x=[10,20,30,40]
y=x
y[1]=777
print(x)
Output
PowerShell
[10,20,30,40]
Cloning of List
- To overcome this problem we should go for cloning.
- The process of creating exactly duplicate independent object is called cloning.
- We can implement cloning by using slice operator or by using copy() function.
Using slice operator
Python
x=[10,20,30,40]
y=x[:]
y[1]=777
print(x)
print(y)
Output
PowerShell
[10,20,30,40]
[10,777,30,40]
Using copy()
Python
x=[10,20,30,40]
y=x.copy()
y[1]=777
print(x)
print(y)
Output
PowerShell
[10,20,30,40]
[10,777,30,40]
Question
Difference between = operator and copy() function
Answer
= operator meant for aliasing and copy() function meant for cloning.
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!