Chapter - 5 List Manipulation
Chapter - 5 List Manipulation
List Manipulation
Sequences in Python
Lists, Dictionaries, Strings, Tuples and Sets
Creating a list
Lists are enclosed in square brackets [ ] and
each item is separated by a comma.
List7=['a','e','i','o','u']
for i in List7:
print(i)
List1=['a','e','i','o','u']
tot=len(List1)
print("Total number of characters are:",tot)
a. Slicing
b. Concatenation
c. Repetition
d. Membership testing
e. Indexing
Syntax:
list[start:stop:step]
start is the starting point.
stop is the stopping point, which is not included.
Step is the step size. It is optional. Default value is 1.
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
a. Slicing
list =['I','N','D','I','A']
>>> x='computer'
>>> x[1:4] # omp
>>> x[3:] # puter
>>> x[:5] # compu
>>> x[-3:] # ter
>>> x[:-2] # comput
list1=['Red','Green']
list2=[10,20]
list1=list1+['Blue']
list2=list2+[30]
list3=list1+list2
print(list3) # ['Red', 'Green', 'Blue', 10, 20, 30]
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
c. Repetition/Replication
Asterisk (*) operator replicated the List for a
specified number of times.
e.g.
list2=[10,20,30]
print(list2*2) [10, 20, 30, 10, 20, 30]
x="Python"*3
print(x) PythonPythonPython
e.g.2
y=[10,20,30,40]
print (50 in y) Output: False
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
d. Membership Testing
student_XII = ['Ritu', 'Saksham', 'Abha', 'Varun']
for name in student_XII:
print(name)
Output: Ritu
Saksham
Abha
Varun
e.g.1
list1=['Red','Green','Blue']
list2=[10,20,30]
Output:
print(list1[-1]) Blue
print(list2[2])
Designed by: Umesh Pun (PGT IP)
30
APS Yol Cantt
3. Built in Functions
Python provides several built-in functions to perform
various operations on list.
a. Append
b. Insert
c. update
d. Remove
e. Sort
f. Reverse
g. Len
h. Count
i. Index
j. Extend
k. Clear
Syntax: list.append(item)
L1=[10,20,30,40]
L1.append(55)
[10, 20, 30, 40, 55]
print(L1)
e.g.2
color=['Red','Green','Blue']
color.append('White')
print(color) ['Red', 'Green', 'Blue', 'White']
e.g.
names=['Ajay','Vinay','Sonia']
names.insert(2,'Sanjay')
print(names)
['Ajay', 'Vinay', 'Sanjay', 'Sonia']
e.g.
List3=[1,2,3]
List3[1]=10
print(List3) [1, 10, 3]
e.g.
names=['Ajay','Vinay','Sonia']
names.remove('Vinay')
print(names) ['Ajay', 'Sonia']
e.g.
L1=[10,4,22,3,100,2]
L1.sort()
print(L1) [2, 3, 4, 10, 22, 100]
Syntax:
list_name.sort(reverse=True)
e.g.
L5=[5,"Python","BPB",190.5]
print(len(L5)) Output: 4
e.g.
L5=[10,20,10,30,10,40]
print(L5.count(10)) Output: 3
x=['USA','UK','INDIA','JAPAN']
print(x.index('INDIA')) Output: 2
e.g.
L1=[10,20,30,40]
L2=[50,60]
L1.extend(L2)
print(L1) [10, 20, 30, 40, 50, 60]
e.g.
L1=[10,20,30,40]
L1.clear()
print(L1) Output: [ ]
a. pop() or del
(if index is known)
b. remove()
(if element is known not the index)
c. del()
(remove more than one element)
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
a. pop() or del
pop()
It removes the item from the specified index and returns
the element which was removed.
e.g.
L1=[10,20,30,40,50]
a=L1.pop(1)
print(L1) #[10, 30, 40, 50]
e.g.
L1=[10,20,30,40,50]
del L1[2]
print(L1) #[10, 20, 40, 50]
e.g.
L1=[10,20,30,40,50]
L1.remove(40)
print(L1) #[10, 20, 30, 50]
e.g.
L1=[10,20,30,40,50]
del L1[2:4]
print(L1) #[10, 20, 50]
e.g.
>>>L1=[‘A’,’B’,’C’]
>>>’D’ in L1
False
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
6. Other Functions
max()
Returns the element with maximum value from
the list.
e.g1.
List3=[10,20,30,50]
print(max(List3)) # 50
e.g2.
List4=['a','A','i','I']
print(max(List4)) # i according to ASCII Value
e.g1.
List3=[10,20,30,50]
print(min(List3)) # 10
e.g2.
List4=['a','A','i','I']
print(min(List4)) # A according to ASCII Value
NumList = [1,33,22,88,4,44]
Number=len(NumList)
print("Length of this List is : ", Number)
smallest = largest = NumList[0]
ctr=0
List = [1,33,22,88,1,4,44,1]
print(List)
num=int(input("Enter Number in the List:"))
if(num= =List[i]):
ctr=ctr+1
L=[]
n=int(input("How many items to be enetered:"))
i=1
while(i<=n):
a=int(input("Enter element:"))
L.append(a)
i=i+1
sum=0
for i in range(0, len(L)):
sum=sum+L[i]
avg=sum/n
L=[10,30,88,27,93]
print(L)
n=int(input("Enter item to be search:"))
flag=0
for i in range(0, len(L)):
if (n==L[i]):
flag=1
break
if (flag==1):
print("Element found")
else:
print("Element not found")
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
# Python Program to remove all items from the
List
L=[10,20,12,44,88]