Chapter - 6 Python Collective List
Chapter - 6 Python Collective List
-Nirali Purohit
It is a collections of items and each item has its own
index value.
Index of first item is 0 and the last item is n-1.Here
n is number of items in a list. i.e. length of the list
Indexing of list
L=
L[1]=Y
L[-3]=H
Creating a list
Lists are enclosed in square brackets [ ] and each item is
separated by a comma. It can be homogeneous or not
Initializing a list
Passing value in list while declaring list is initializing of a list
e.g.
list1 = [‘English', ‘Hindi', 1997, 2000,True]
list2 = [11, 22, 33, 44, 55 ]
list3 = ["a", "b", "c", "d"]
Blank list creation
A list can be created without element
List4=[ ]
List5=list() #constructor method
Access Items From A List
List items can be accessed using its index position.
e.g.
list =[3,5,9] 3
print(list[0]) 5
print(list[1]) 9
print(list[2]) Negative indexing
output 9
print('Negative indexing') 5
print(list[-1]) 3
print(list[-2])
print(list[-3])
Iterating/Traversing Through A List
List elements can be accessed using looping statement.
e.g.
# i as index
list =[3,5,9]
for i in range(0, len(list)):
print(list[i])
# i as element
for i in list:
print(i)
Output
3
5
9
Slicing of A List
List elements can be accessed in subparts.
e.g.
list =['I','N','D','I','A']
print(list[0:3])
print(list[3:])
print(list[:])
Output
['I', 'N', 'D']
['I', 'A']
['I', 'N', 'D', 'I', 'A']
Updating / Manipulating Lists
We can update single or multiple elements of lists by giving
the slice on the left-hand side of the assignment operator.
e.g.
list = ['English', 'Hindi', 1997, 2000]
print ("Value available at index 2 : ", list[2])
list[2:3] = 2001,2002
list[2]=2001 for single item update
print ("New value available at index 2 : ", list[2])
print ("New value available at index 3 : ", list[3])
Output
('Value available at index 2 : ', 1997)
('New value available at index 2 : ', 2001)
('New value available at index 3 : ', 2002)
Add Item to A List
append() method is used to add an Item to a List.
e.g. list=[1,2]
print('list before append', list)
list.append(3)
List.append([1,2])
#Will append [1,2] as one element of list
print('list after append', list)
Output
('list before append', [1, 2])
('list after append', [1, 2, 3,[1,2]])
Output
e.g.
del list[0:2] # delete first two items
del list # delete entire list
del(list) #also deletes entire list
Basic List Operations
Python Expression Results Description
Build in methods :
Function Description
len(list) Return total length of the list.
max(list) Return item with maximum value in the list.
min(list) Return item with min value in the list.
sum(list) Return the sum of integers and boolean True or False
sorted(list) Returns after sorting the list. sorted(list,reverse=True) =desc sorted
del(list) Deletes the list. Syntax error if try to return
Build in methods :
Function Description
len(list) Return total length of the list.
max(list) Return item with maximum value in the list containing
integer and Boolean values
min(list) Return item with min value in the list containing integer and
Boolean values
sum(list) Return the sum of integers and Boolean values
sorted(list) Sorts the list and returns it . List should be homogeneous.
sorted(list,reverse=True) Descending if reverse=True
del(list) or del list Deletes the list and does not even return None
L=[1,2,True,False]
len(L) #4
max(L) #2
min(L) #0
sum(L) #4
sorted(L) # [False,1,True,2]
del(L) # Deletes the list and does not return anything not even None
(syntax error if try to return)
NameError thrown if list not found for all the methods
Important methods and functions of List
Function Description Remarks
list.append(ele) Add an Item at end of a list Iteratives also added as one element
list.extend(iterative) Add multiple Items at end TypeError if not iterable value
list.insert(index,ele) insert an Item at a defined index If index bigger than last index,adds last
list.remove(ele) remove an Item from a list ValueError if element not in list
list.clear() Empties the list No error if called again and again
list.sort() Sort the items of a list in List should be homogenous : string or
l.sort(reverse=True) ascending or descending order interger(bool+float+int) or list or tuple
list.reverse() Reverse the items of a list Returns None
Lst.copy() Returns shallow copy of the Lst.L2=Lst.copy() #both L2 and Lst have
(Different ids like list()) different ids
L2=Lst #Both L2 and LST have same
NameError thrown for all the methods
ids if list not present
All the methods return None (as they just do something)
Can call them methods which do something and does
not return anything but None
Important methods and functions of List
Function Description Remarks
List.index(element) Returns the index of the element ValueError if element not in range
list.pop() Return the removed element and
IndexError if index not in range for
List.pop(index) remove the element from the
pop(index)
defined index for pop(index).
Removes last element for pop()
list.count(ele) Return number of times element
present in the list. Count of list 0 returned if element not in list
takes only one argument
Output
Average of the list = 35.75
Note : The inbuilt function mean() can be used to calculate the mean( average ) of
the list.e.g. mean(list)
Some Programs on List
Linear Search
Some Programs on List
Bubble Sorting
Some Programs on List
Frequency of an element in list
OUTPUT
Original List : [101, 101,101, 101, 201, 201, 201, 201]
{101: 4, 201:4}
OUTPUT
[1, 4, 6] [2, 5]