0% found this document useful (0 votes)
11 views4 pages

Python List Manipulation

Uploaded by

yogesh2522004
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)
11 views4 pages

Python List Manipulation

Uploaded by

yogesh2522004
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/ 4

7.

LIST MANIPULATION
List in python used to store sequence of items belonging to any type.
[ ] Square brackets are used in list
Each item is separated by comma
List are mutable (elements in a list can be changed)

Consider the following list


negative index
-6 -5 -4 -3 -2 -1
mylist = 9 19 29 30 60 65
0 1 2 3 4 5
index

index - from left hand side


starts from 0 to size – 1
negative index - start right
from -1 to –(size)

Creating List
List1 = [10, 15, 20, 25, 30]
List2 = [‘a’, ‘e’, ‘I’, ‘o’, ‘u’]
List3 = [2202, “Amit”, 25, “India”, “Pass”, 25.6]
List4 = [“MP”, “UP”, “CG”, “AP”]

List5 = list(“kvs”)
List5 # [‘k’, ‘v’, ‘s’]

Access an item from a list


Item in a list can be access by using its either index number or negative index number

ListA = [8, 19, 23, 47]


ListA[0] #8
ListA[2] # 23
ListA[‐2] # 23

Iteration through a list


O/p 2
ListB = [2, 4, 8, 10]
for i in range(0, len(listB)) : 4
print(listB[i]) 8
10

Slicing of a list
ListC = [“I”, “N”, “I”, “D”, “I”, “A”]
ListC[0 : 3] # [‘I’, ‘N’, ‘D’]
ListC[1 : 4] # [ ‘N’, ‘D’, ‘I’]
ListC[ : ]
ListC Both will give same result

# [ ‘I’, ‘N’, ‘D’, ‘I’, ‘A’]

Youtube channel : ipcsguide 1 Manish Kumar Gupta


Updating a list
Single or Multiple elements can be updated.
ListD = [36, 46, 56, 66, 76]
ListD[2] # 56
ListD[2] = 1000 # 56
ListD[2] # 1000
ListD # [36, 46, 1000, 66, 76]
ListD[2:3]= 400,500 # [36, 46, 400, 500, 66, 76]

Add an item to a list


There are two ways to add an item to a list
a. Using append
b. Using Extend

Using append : It is used to add only one item at a time


Using Extend : It is used to add one or more item (Multiple items)at a time

Append command Extend command


ListF = [44, 55] ListF = [44, 55]
List F # [44, 55] List F # [44, 55]
ListF.append(77) ListF.extend(77) # Error
ListF # [44, 55, 77]
ListF.extend([77]) # [44, 55, 77]
ListF.append(88,88) # Error
ListF.append([88, 99]) # [44, 55, 77, 88, 99]

Add Two Lists


Two or more lists can be added.

ListM = [8, 9, 10]


ListN = [20, 30]

List1 = ListM + listN


List1 # [8, 9, 10, 20, 30]

List2 = ListN + listM


List2 # [20, 30, 8, 9, 10]

ListP = [100,200 ]
List4 = ListP + ListM + ListN # [100, 200, 8, 9, 10, 20, 30]

Delete an item from a List


One or more items is possible to delete from a list.

ListB = [11, 21, 31, 41, 51]


del listB[1] # [11, 31, 41, 51]
del ListB[0 : 2] # used to delete first 2 items from list
# [41, 51]
del ListB # Delete the entire list
ListB # Error

Youtube channel : ipcsguide 2 Manish Kumar Gupta


“*” Command in a List
“*” function used in a list to repeat the item of list
ListC = [21, 31, 41]
ListC # [21, 31, 41]
ListC * 3 # [21, 31, 41, 21, 31, 41, 21, 31, 41 ]

Whether item present in a List


in ‐ return True if item present in the list otherwise return False
not in ‐ return True if item is not available in the list otherwise return True

ListC = [21, 31, 41]


10 in ListC # False
31 in ListC # True

15 not in ListC # True


41 not in ListC # False

Sequence
List1 = list(“good”)
List1 # [‘g’, ‘o’, ‘o’, ‘d’]

List2 = list(input(‘Enter the list of elements : ‘))


Enter the list of Elements : 23456
List2 # [‘2’, ‘3’, ‘4’, ‘5’, ‘6’] as string

FUNCTIONS
a. insert( )
this function is used to insert an item to the list at desired index position.
ListC = [21, 31, 41, 51, 61, 71]
listC.insert(2, 100) # [21, 31, 100, 41, 51, 61, 71]

b. remove( )
Function is used to remove an item from the list, argument will be item
ListC = [21, 31, 41, 51, 61, 71]
listC.remove(41) # [21, 31, 51, 61, 71]

c. pop( )
Function is used to remove an item from the list, argument will be index number
ListC = [21, 31, 41, 51, 61, 71]
listC.pop(3) # 51
listC # [21, 31, 61, 71]

d. index( )
Function return the index number of any item available in the list
ListC = [21, 31, 41, 51, 61, 71]
listC.index(51) # 3 (index number of 51 is 3)

e. clear( )
Function is used to remove all items from list or make the list empty.
List will be existed.
ListC = [21, 31, 41, 51, 61, 71]
listC.clear( ) #[]

Youtube channel : ipcsguide 3 Manish Kumar Gupta


f. count( )
Function is used to count the number of occurrence of any element in a list.
ListC = [1, 3, 5, 1, 7, 2, 1, 7]
listC.count(1) #3
listC.count(7) #2
listC.count(100) #0

g. max( )
Return the maximum item value in a list of items
ListC = [1, 3, 5, 1, 17, 2, 1, 7]
max(ListC ) # 17

h. min( )
Return the maximum item value in a list of items
ListC = [1, 3, 5, 1, 17, 2, 1, 7]
min(ListC ) #1

i. len( )
count the number of element available to the list
ListC = [11, 22, 33, 44, 55]
len(ListC ) #5

ListB = [“Ram”, “Kamal”, “Anuj”]


ListB # [‘Ram’, ‘Kamal’, ‘Anuj’]
len(ListB) #3

j. reverse( )
show the list of items in reverse order
ListC = [11, 22, 33, 44, 55]
ListC.reverse( )
ListC # 55, 44, 33, 22, 11

k. sort( )
arrange the list of item in an order
ListD = [4, 6, 2, 8, 1, 3, 11]
ListC.sort( )
ListC # 1, 2, 3, 4, 6, 8, 11

Youtube channel : ipcsguide 4 Manish Kumar Gupta

You might also like