LIST MANIPULATION Practical Live Class
LIST MANIPULATION Practical Live Class
In [1]: a=list('informatics')
print(a)
['i', 'n', 'f', 'o', 'r', 'm', 'a', 't', 'i', 'c', 's']
In [3]: a=list((10,20,30,40,50))
print(a)
In [9]: print(fruits[-2])
pomegranate
In [10]: print(fruits[4])
cherry
In [11]: print(fruits[6])
-------------------------------------------------------------------------
--
IndexError Traceback (most recent call las
t)
<ipython-input-11-a5ee2976c09a> in <module>()
----> 1 print(fruits[6])
Traversing a list
In [14]: L=[1,2,55,34,67,23,11]
for a in L:
print(a,end=" ")
1 2 55 34 67 23 11
Comparing List
In [15]: 10>20
Out[15]: False
In [16]: 20==20
Out[16]: True
In [17]: [10,20]>[10,20,30]
Out[17]: False
In [18]: [10,20,30,40]>[10,20,30]
Out[18]: True
In [19]: [10,20]>[50,20]
Out[19]: False
In [20]: [1,3,5]<[1,5,3]
Out[20]: True
In [22]: [10,20,60,80]>[10,20,30,100]
Out[22]: True
Joining of lists
In [1]: a=[1,2,3]
b=[10,20,30]
c=a+b
print(c)
In [2]: [1,2,3]+['a','b']
In [3]: ['a','b']+20
-------------------------------------------------------------------------
--
TypeError Traceback (most recent call las
t)
<ipython-input-3-b22b00fcdb37> in <module>()
----> 1 ['a','b']+20
Repeating a list
In [4]: list1=[11,'a','b']
print(list1*2)
In [6]: list1=[11,'a','b']
print(list1*3.5)
-------------------------------------------------------------------------
--
TypeError Traceback (most recent call las
t)
<ipython-input-6-d0eec9b9faff> in <module>()
1 list1=[11,'a','b']
----> 2 print(list1*3.5)
In [9]: marks=[34,12,33,26,21,9,11]
print(marks[1:4:1])
print(marks[1:4]) #step is 1 by default
In [10]: print(marks[0:6])
In [11]: marks[0:10]
In [12]: marks
In [13]: marks[0:10:2]
In [16]: marks[5:1]
Out[16]: []
In [17]: marks[5:1:-1]
In [18]: marks
In [2]: marks
In [4]: marks[::]
In [6]: marks[::3]
In [8]: list1[3]="bhavika"
In [9]: list1
In [11]: list1[1:4]=['Ram','Shyam']
In [12]: list1
list()
append()
In [1]: L=[10,20,30]
L.append(40)
print(L)
extend()
In [4]: x=[11,55,77,99]
x.extend([100,200]) #here data is passed in list form
print(x)
insert()
In [5]: L=[10,20,30,40,50]
L.insert(3,100) #here 3 is index, 100 is value to be inserted
print(L)
In [6]: L.insert(len(L),200)
print(L)
In [8]: L=[10,20]
L.pop()
L.pop()
print(L)
[]
In [9]: L.pop()
-------------------------------------------------------------------------
--
IndexError Traceback (most recent call las
t)
<ipython-input-9-a522295e58ee> in <module>()
----> 1 L.pop()
remove()
In [10]: L=[10,20,30,40,50]
L.remove(30)
print(L)
In [14]: L=[10,20,30,40,50,30,100]
L.remove(30)
print(L)
-------------------------------------------------------------------------
--
ValueError Traceback (most recent call las
t)
<ipython-input-15-894e20f92058> in <module>()
1 L=[10,20,30,40,50,30,100]
----> 2 L.remove(80)
3 print(L)
reverse
In [2]: L1=[10,20,30,40]
L2=[11,3,5,12,1]
L1.reverse()
print(L1)
In [3]: L3=L2.reverse()
print(L3)
None
In [4]: print(L2)
In [5]: items=[10,20,30,40,50,60]
del items[2]
print(items)
In [6]: items=[10,20,30,40,50,60]
del items[2:5]
print(items)
-------------------------------------------------------------------------
--
NameError Traceback (most recent call las
t)
<ipython-input-8-a6831a2a447a> in <module>()
2 print(items)
3 del items
----> 4 print(items)
In [10]: a[2]=88
print(a)
In [11]: print(b)
In [12]: a=[10,30,50,70,90,110]
b=list(a)
print(a)
print(b)
In [13]: a[1]=11
print(a)
min() function
In [2]: L1=[33,10,1,44,23,100]
print(min(L1))
In [4]: L1=[33,23,100,"ip","cs"]
print(min(L1))
-------------------------------------------------------------------------
--
TypeError Traceback (most recent call las
t)
<ipython-input-4-9693ebc691c6> in <module>()
1 L1=[33,23,100,"ip","cs"]
----> 2 print(min(L1))
In [7]: L1=[33,23,100,11.5,23.6,-10,-40]
print(min(L1))
-40
In [8]: x=1
L1=[x,33,23]
print(min(L1))
max() method
In [9]: L1=[33,10,1,44,23,100]
print(max(L1))
100
sum() method
In [10]: L1=[33,10,1,44,23,100]
print(sum(L1))
211
In [11]: L1=[1,2,3]
print(sum(L1))
In [12]: L1=[33,-40,1.5]
print(sum(L1))
-5.5
In [13]: L1=[33,-40,1.5,"a"]
print(sum(L1))
-------------------------------------------------------------------------
--
TypeError Traceback (most recent call las
t)
<ipython-input-13-cb6513d3d9bd> in <module>()
1 L1=[33,-40,1.5,"a"]
----> 2 print(sum(L1))
Enter a list:[10,34,23,56,88]
Largest element in a list: 88
Smallest element in a list: 10
In [5]: list1=eval(input("Enter a list:"))
largest=list1[0] #10
for i in list1: #i=10,23,45,1,78,34
if i>largest: #10>10, 23>10, 45>23
largest=i #23, 45
print("Largest value is:",largest)
smallest=list1[0]
for i in list1:
if i<smallest:
smallest=i
print("Smallest value is:",smallest)
Enter a list:[10,23,45,1,78,34]
Largest value is: 78
Smallest value is: 1
String programs
In [ ]: