List in Python
List in Python
-> List is grow able in nature therefore based on incoming flow of data ,
the size of a list is getting increased or decreased.
-> List is mutable in nature hence values in the list can be modified ,
which means that Python will not create a new list when you make
changes to an element of a list.
lt=[2,4,5]
lt1=['hello','hi','bye']
lt2=[25.7,35.8,93.2]
print (lt)
print (lt1)
print (lt2)
output:
[2, 4, 5]
['hello', 'hi', 'bye']
[25.7, 35.8, 93.2]
>>>for i in List:
print(i,end="#")
O/P
1#2#3#4#
By using loop:
List=[1,2,3,4,5]
for i in range(0, len(List)):
print(list[i],end=“ “)# 1 2 3 4 5
Access Elements
We can access list elements by using index and slicing.
The index operator [] is used to access an item in a list. Index starts from 0.
lt=[2,4,5]
lt1=['sun','mon','tue']
lt2=[['one','two','three'],[4,5,6]]
print(lt[0])
print(lt1[-2])
print(lt2[0][1])
print(lt2[-1][-1])
print(lt2[0][1])
lt=[2,4,5]
lt2=[['one','two','three'],[4,5,6]]
for i in lt:
print(i)
for i in lt2:
for j in i:
print(j,end="")
print()
Nested List: A list inside another list is called Nested List.
Examples:
>>> L=[1,2,[4,5],7]
>>> print(L)
>>> L[0]
>>> L[-2]
[4, 5]
>>> L[-2][0]
>>> L[-2][1]
>>> L[-2][-1]
>>> L[-2][0]
>>> L
>>> L[3]=9
>>> L
[1, 2, [4, 5], 9]
Examples:
X=[1,7,[5,6],10]
Y=list(X)
print(X,Y)
X[2][0]=7
print(X,Y)
X[0]=43
print(X,Y)
Slicing a list: List slices are the sub-parts of a list extracted out. List slices can
be created through the use of indexes. Slicing is used to retrieve a subset of
values.
Syntax: list[start:end:step]
Examples:
>>> list1=[24,56,10,99,28,90,20,22,34]
>>> slice1=list1[0:6:1]
>>> print(slice1)
[24, 56, 10, 99, 28, 90]
>>> slice2=list1[3:-3]
>>> print(slice2)
>>> print(list1)
>>> list1[3]=100
>>> print(list1)
>>> list1[0:7:3]
>>> list1[:]
>>> list1[::]
If step is –ve:
For examples :
>>> list1[-1::]
[34]
>>> list1[::-1]
>>> list1[:-5:-2]
[34, 20]
>>> L=[10,20,30,40,50]
>>> L[0:len(L):1]
>>> L[0:]
>>> L[0:len(L)]
>>> L[1:5]
>>> L[1:5:0]
L[1:5:0]
>>> L[1:5:2]
[20, 40]
>>> L[::-1]
>>> L[0:5:-1]
[]
>>> L[-4:-1:-1]
[]
>>> L[-4:-1]
>>> L[-5:3:1]
For examples:
Q. Write a program to take list as input , adds 5 in all the odd values and 10 in
all the even values of the list L. Also display the list
L=[10,20,3,100,65,87,2]
print("old list",L)
for i in range(0,len(L)):
if L[i]%2==0:
L[i]=L[i]+10
else:
L[i]=L[i]+5
print("New list:",L)
Q. WAP to find and display the sum of all the values which are ending with 3
from a list?
Suppose the list is [33,13,92,99,3,12]
L=[33,13,92,99,3,12]
sum=0
x=len(L)
for i in range(0,x):
if L[i]%10==3:
sum=sum+L[i]
print("the sum of the list ",sum)
List function/method:
1. append(): It adds a single item to the end of the list. It doesn’t create a new
list; rather it modifies the original list.
Syntax: list.append(item)
for
examples:
.
>>> list1=[1,2,3,4,5,6]
>>> print(list1)
[1, 2, 3, 4, 5, 6]
>>> id(list1)
37124496
>>> list1.append(7)
>>> print(list1,id(list1))
[1, 2, 3, 4, 5, 6, 7] 37124496
>>> L=[]
>>> L1=[]
>>> L2=[]
>>> print(L,L1,L2)
[] [] []
>>> for i in range(6,10):
L.append(i)
>>> print(L)
[6, 7, 8, 9]
>>> print(L1)
[]
>>> for i in range(10,4,-2):
L1.append(i)
>>> print(L1)
[10, 8, 6]
>>> print(L)
[6, 7, 8, 9]
>>> print(L1)
[10, 8, 6]
>>> L2
[]
>>> for i in range(len(L1)):
L2.append(L[i]+L1[i])
>>> print(L2)
[16, 15, 14]
>>> L2.append(len(L)-len(L1))
>>> L2
[16, 15, 14, 1]
2. extend() method: extends() method adds one list at the end of another list.
In other words, all the items of a list are added at the end of an already
created list.
Syntax: list1.extend(list2) # where list2 are the list which will be extended
For examples:
>>> L1=[100,200,300,400]
>>> L2=[10,20,30]
>>> print(L1,L2]
>>> print(L1,L2)
[100, 200, 300, 400] [10, 20, 30]
>>> L1.extend(L2)
>>> print(L1)
[100, 200, 300, 400, 10, 20, 30]
>>> L1.extend([80,90])
>>> L1
[100, 200, 300, 400, 10, 20, 30, 80, 90]
For example 2:
>>> L1
[100, 200, 300, 400, 10, 20, 30, 80, 90]
>>> print(L1)
[100, 200, 300, 400, 10, 20, 30, 80, 90]
>>> L1.extend([[500,600]])
>>> print(L1)
[100, 200, 300, 400, 10, 20, 30, 80, 90, [500, 600]]
>>> L1.append([700,800])
>>> L1
[100, 200, 300, 400, 10, 20, 30, 80, 90, [500, 600], [700, 800]]
>>> L1
>>> L1.insert(1,30)
>>> print(L1)
>>> print(L)
>>> L[0]
>>> L[-3]
10
>>> L.insert(2,3)
>>> print(L)
>>> L.insert(4,11)
>>> L
>>> L.insert(0,0)
>>> L
For examples:
1.
List1=[10,15,20,25,30]
List1.insert(3,4)
List1.insert(2,3)
print(List1[-5])
(i) 2 (ii)3 (c) 4 (d) 20
x=[[10.0,11.0,12.0],[13.0,14.0,15.0]]
y=x[1][2]
print(y) #15.0
print(x[-2][1])#11.0
Delete Operations:
Python provides operator for deleting/removing an item from a list. There are
many methods for deletions.
pop(): It removes the elements from the specified index and also returns the
element which was removed. pop() removes last element if the index is not
specified.
Syntax: List.pop(index)
Examples:
>>> L1=[1,2,5,4,70,10,90,80,50]
>>> a=L1.pop(1)
>>> a
2
>>> L1
[1, 5, 4, 70, 10, 90, 80, 50]
>>> print(L1.pop(-2))
80
>>> L1
[1, 5, 4, 70, 10, 90, 50]
>>> L1.pop()
50
>>> L1
[1, 5, 4, 70, 10, 90]
>>> L1.pop()
90
>>> L1
[1, 5, 4, 70, 10]
>>> L1=[1,2,5,4,70,10,90,80,50]
>>> L1.pop(3)
4
>>> L1
[1, 2, 5, 70, 10, 90, 80, 50]
>>> L1.pop()
50
>>> L1
[1, 2, 5, 70, 10, 90, 80]
>>> L1.pop(-2)
90
>>> L1
[1, 2, 5, 70, 10, 80]
Note: If you don’t pass any index, then pop() method removes last element
from the list.
del statement:
del statement removes the specified element from the list but not return the
deleted values. del statement also destroy whole list object.
>>> L1=[100,200,300,400,500]
>>> print(L1)
[100, 200, 300, 400, 500]
>>> del L1[3]
>>> print(L1)
[100, 200, 300, 500]
>>> del(L1[1:3])
>>> print(L1)
[100, 500]
sort() method: This function sorts the item of the list, by default in
ascending/increasing order.
>>> L2=[10,4,20,98,2,100]
>>> L2.sort()
>>> L2
[2, 4, 10, 20, 98, 100]
# in descending order
>>> L3=[10,4,20,98,2,100]
>>> L3.sort(reverse=True)
>>> L3
[100, 98, 20, 10, 4, 2]
Example:
>>> L=[1,9,2,3,7,5]
>>> L.sort()
>>> L
[1, 2, 3, 5, 7, 9]
>>> L1=["Bob","Aman","Chetan","Ashish","Debasis"]
>>> L1.sort()
>>> L1
['Aman', 'Ashish', 'Bob', 'Chetan', 'Debasis']
>>> L.sort(reverse=True)
>>> L
[9, 7, 5, 3, 2, 1]
>>> L1.sort(reverse=True)
>>> L1
['Debasis', 'Chetan', 'Bob', 'Ashish', 'Aman']
clear() method: The clear() method removes all the items from the list. This
method doesn’t take any parameters. The clear() method only empties the
given list.
>>> L1=[10,20,30,40]
>>> print(L1)
[10, 20, 30, 40]
>>> L1.clear()
>>> print(L1)
[]
Homework
Q. Write the most appropriate list method to perform the following task:
(i) Delete a given element from the list
Sol: remove()
>>> L2=[1,2,3]
>>> L2[1]=4
>>> L2
[1, 4, 3]
count() method: count() method counts how many times an element has occurred in a list
and returns it.
Syntax: list.count(element)
For examples:
>>> L=[10,20,30,40,50]
>>> L.count(10)
1
>>> L.extend([10,70,90])
>>> L
[10, 20, 30, 40, 50, 10, 70, 90]
>>> L.count(10)
2
index(): List can easily be searched for values using the index() method that expects a
value that is to be searched.
List_name.index(element)
>>> list=[100,200,50,400]
>>> list.index(200)
1
>>> list.index(500)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
list.index(500)
ValueError: 500 is not in l
Q.1 Write a program to create a list and perform following operation
For examples: if the list is: [1,45,22,3,20]
The output should be
The sum of even: 42
The odd is : 49
Answer:
L=[1,45,22,3,20]
length=len(L)
soe,sod=0,0
for i in range(length):
if L[i]%2==0:
soe=soe+L[i]
else:
sod=sod+L[i]
print("sum of even",soe)
print("sum od odd",sod)
max(): Returns the element with the maximum values from the list.
Syntax: max(<list>)
Examples:
L=[10,2,3,1,5,10,7,8,20,10]
print(max(L))
>>> L=['Ajay','suman','Raj','aman','Bob']
>>> max(L)
'suman'
Note: max(), min(), sum() never work for mixed type list.
min(): Returns the element with the minimum values from the list.
>>> L=['Ajay','suman','Raj','aman','Bob']
>>> min(L)#’Ajay’
Homework
2. What is the output of the following code ? [CBSE Board Exam 2021]
L=[]
for i in range(4):
L.append(2*i+1)
print(L[::-1])
Q. 5 Which of the following can add an element at any index in the list?
i. insert( ) ii. append( ) iii. extend() iv. all of these
6.What will be the output of the following code:
>>>l1=[1,2,3]
>>>l1.append([5,6])
>>>l1
i. [1,2,3,5,6] ii. [1,2,3,[5,6]] iii. [[5,6]] iv. [1,2,3,[5,6]]
sorted():Python sorted() function returns a sorted list from the iterable object.
Syntax: sorted(iterable, key, reverse)
Example:
L=[4,5,1,2,7,6]
>>> print(sorted(L))
[1, 2, 4, 5, 6, 7]
>>> print(sorted(L,reverse=True))
[7, 6, 5, 4, 2, 1]