[go: up one dir, main page]

0% found this document useful (0 votes)
144 views45 pages

Chapter - 5 List Manipulation

[10, 30, 40, 50] del It deletes the element from the specified index. e.g. L1=[10,20,30,40,50] del L1[1] print(L1)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views45 pages

Chapter - 5 List Manipulation

[10, 30, 40, 50] del It deletes the element from the specified index. e.g. L1=[10,20,30,40,50] del L1[1] print(L1)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Chapter - 5

List Manipulation

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
Introduction
Sequence in an object that contain multiple items of
data.

A sequence may have repeated items in the list. The


number of elements are called length of the sequence.

Sequences in Python
Lists, Dictionaries, Strings, Tuples and Sets

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
1. Lists
A list is a collection of items and each item has its own
index value. The items in a list can be of any type such
as strings, integers, floats.

Items in a list can be of multiple types. The


items/elements are enclosed in Square brackets [ ].

Index of first item is 0 and the last item is n-1.


Here n is number of items in a list.

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
a. Creating a List
The syntax for creating a list is:
<list_name> = [ ]

Creating a list
Lists are enclosed in square brackets [ ] and
each item is separated by a comma.

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
b. Accessing Items from a List

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
c. Travesing a List
Traversing means accessing each item in the
list.
This can be done using loops.

Using ‘in’ operator inside the loop

List7=['a','e','i','o','u']
for i in List7:
print(i)

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
Travesing a List
Using ‘range’ function

List1=['a','e','i','o','u']

for i in range(0, len(List1)):


print(List1[i])

tot=len(List1)
print("Total number of characters are:",tot)

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
2. Operations on Lists
Python provides several basic operations
which can be performed on a list.

a. Slicing
b. Concatenation
c. Repetition
d. Membership testing
e. Indexing

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
a. Slicing
Slicing is an operation in which you can slice a
particular range from that sequence.

List slices are the sub-parts of a list extracted out.

List slices can be created through the use of


indexes.

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']

print(list[:]) ['I', 'N', 'D', 'I', 'A']


print(list[0:3]) ['I', 'N', 'D']
print(list[2:]) ['D', 'I', 'A']
print(list[:3]) ['I', 'N', 'D']
print(list[-3:]) ['D', 'I', 'A']
print(list[:-4]) ['I']
Print(list[1:4:2]) [‘N‘,'I']
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
a. Slicing
Consider a list/sequence as:

>>> x='computer'
>>> x[1:4] # omp
>>> x[3:] # puter
>>> x[:5] # compu
>>> x[-3:] # ter
>>> x[:-2] # comput

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
b. Concatenation
Multiple sequences/lists can be combined together with help
of certain operators.
Python allows adding two lists using ‘+’ operator.

list1=['Red','Green']
list2=[10,20]

list1=list1+['Blue']
list2=list2+[30]

print(list1) # ['Red', 'Green', 'Blue']


print(list2) # [10, 20, 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

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
d. Membership Testing
Membership testing is an operation carried out
to check whether a particular element/item is a
member of that sequence or not.
e.g.1
x='Python'
print ('o' in x) Output: True

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

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
e. Indexing
Index is a number specifying the position of an
element in a list. It enables access to individual
elements in the list.

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

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
a. append()
append() method 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)

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
a. append()
e.g1

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']

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
a. append()
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
print(L)

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
b. insert()
insert() function can be used to insert
an element at a specified index.
Syntax:
list_name.insert(index_number, value)

e.g.
names=['Ajay','Vinay','Sonia']
names.insert(2,'Sanjay')
print(names)
['Ajay', 'Vinay', 'Sanjay', 'Sonia']

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
c. update()
upadte() function can be used to assign
a new value to an lready exisiting value
using assignment operator (=).
Syntax:
list_name[index]=<new value>

e.g.
List3=[1,2,3]
List3[1]=10
print(List3) [1, 10, 3]

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
d. remove()
remove() function can be used to
remove an element at a specified
index.
Syntax:
list_name.remove(value)

e.g.
names=['Ajay','Vinay','Sonia']
names.remove('Vinay')
print(names) ['Ajay', 'Sonia']

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
e. sort()
sort() function sorts the items os the
list in ascending or descending order.
Syntax:
list_name.sort()

e.g.
L1=[10,4,22,3,100,2]
L1.sort()
print(L1) [2, 3, 4, 10, 22, 100]

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
e. sort()
e.g.

Syntax:
list_name.sort(reverse=True)

#sort in descending order

names=['Ajay', 'Vinay', 'Sanjay', 'Sonia']


names.sort(reverse=True)
print(names) ['Vinay', 'Sonia', 'Sanjay', 'Ajay']

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
f. reverse()
reverse() function in python reverses the
order of the elements in a list. It replaces
a new value ‘in place’ of an item that
already exists in the list.
Syntax:
list_name.reverse()
e.g.
L2=['Sam', 'Aman']
L2.reverse()
['Aman', 'Sam']
print(L2)
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
g. len()
len() function returns length of the list.
Syntax:
Len(list_name)

e.g.
L5=[5,"Python","BPB",190.5]
print(len(L5)) Output: 4

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
h. count()
count() function counts and returns
how many times an element has
occurred in a list.
Syntax:
List.count(element)

e.g.
L5=[10,20,10,30,10,40]
print(L5.count(10)) Output: 3

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
i. index()
index() function returns the index of first
matched item from the list.
Syntax:
List.index(item)
e.g.
L5=[10,20,10,30,10,40]
print(L5.index(10)) Output: 0

x=['USA','UK','INDIA','JAPAN']
print(x.index('INDIA')) Output: 2

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
j. extend()
extend() function adds one list at the
end of another list.
Syntax:
list1.extend(list2)

e.g.
L1=[10,20,30,40]
L2=[50,60]
L1.extend(L2)
print(L1) [10, 20, 30, 40, 50, 60]

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
k. clear()
clear() removes all the items from the
list.
Syntax:
list1.clear()

e.g.
L1=[10,20,30,40]
L1.clear()
print(L1) Output: [ ]

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
4. Deletion Operation
Python provides methods for deleting
item from the list.

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]

Note:L1.pop() – if no index value is provided last


element is deleted.

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
a. pop() or del
del()
It removes the item from the specified index but does
returns the element which was removed.

e.g.
L1=[10,20,30,40,50]
del L1[2]
print(L1) #[10, 20, 40, 50]

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
b. remove()
It is used when the element to be deleted is known.

e.g.
L1=[10,20,30,40,50]
L1.remove(40)
print(L1) #[10, 20, 30, 50]

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
c. del()
It removes more than one element. Here list slice can be
used.

e.g.
L1=[10,20,30,40,50]
del L1[2:4]
print(L1) #[10, 20, 50]

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
5. Searching list
e.g.
>>>L1=[10,20,30,40]
>>>30 in L1
True

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

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
6. Other Functions
min()
Returns the element with maximum value from
the list.

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

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
SUMMARY

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
# Python Program to find Largest and Smallest Number in a
List

NumList = [1,33,22,88,4,44]

Number=len(NumList)
print("Length of this List is : ", Number)
smallest = largest = NumList[0]

for i in range(1, Number):

if(smallest > NumList[i]):


smallest = NumList[i]

if(largest < NumList[i]):


largest = NumList[i]

print("The List is:",NumList)


print("The Smallest Element in this List is : ", smallest)
print("The Largest Element in this List is : ", largest)
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
# Python Program to count occurrence of a Number in the List

ctr=0
List = [1,33,22,88,1,4,44,1]
print(List)
num=int(input("Enter Number in the List:"))

for i in range(0, len(List)):

if(num= =List[i]):
ctr=ctr+1

print("Number of times element occurring in List is:",ctr)

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
# Python Program to calculate mean of elements in the List

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

print("The List is",L)


print("The Mean
Designed by: Umesh
APS Yol Cantt
ofIP)the List is",avg)
Pun (PGT
# Python Program to perform linear search in a List

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]

for i in range(0, len(L)):


del L[i]

print(“All items removed”)


print(L)

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt

You might also like