list and sorting techniques13
list and sorting techniques13
syllabus
2021-22
Chapter 13
List
&
Sorting Techniques
Computer Science
Class XI ( As per CBSE Board)
Visit : python.mykvs.in for regular updates
LIST
Indexing of list
Creating a list
Lists are enclosed in square brackets [ ] and each item is
separated by a comma.
Initializing a list
Passing value in list while declaring list is initializing of a list
e.g.
list1 = [‘English', ‘Hindi', 1997, 2000]
list2 = [11, 22, 33, 44, 55 ]
list3 = ["a", "b", "c", "d"]
Blank list creation
A list can be created without element
List4=[ ]
Visit : python.mykvs.in for regular updates
LIST
list =[3,5,9]
for i in range(0, len(list)):
print(list[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']
e.g.
list=[1,2]
print('list before append', list)
list.append(3)
print('list after append', list)
Output
('list before append', [1, 2])
('list after append', [1, 2, 3])
NOTE :- extend() method can be used to add multiple item at
a time in list.eg - list.extend([3,4])
Visit : python.mykvs.in for regular updates
LIST
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)
print('list after append', list)
Output
('list before append', [1, 2])
('list after append', [1, 2, 3])
OUTPUT
[1,2,3,4]
Output
e.g.
del list[0:2] # delete first two items
del list # delete entire list
Visit : python.mykvs.in for regular updates
LIST
Basic List Operations
# Driver Code
lst = [15, 9, 55, 41, 35, 20, 62, 49]
average = Average(lst)
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)
Visit : python.mykvs.in for regular updates
LIST
found = False
for i in range(len(list_of_elements)):
if(list_of_elements[i] == x):
found = True
print("%d found at %dth position"%(x,i))
break
if(found == False):
print("%d is not in list"%x)
OUTPUT
Original List : [101, 101,101, 101, 201, 201, 201, 201]
Frequency of the elements in the List : Counter({101: 4, 201:4})
1. Bubble Sort
2. Insertion Sort
1. Bubble Sort-
It is one of the simplest sorting algorithms. The two adjacent
elements of a list are checked and swapped if they are in wrong
order and this process is repeated until the whole list elements
are sorted. The steps of performing a bubble sort are:
1. Compare the first and the second element of the list and swap
them if they are in wrong order.
2. Compare the second and the third element of the list and swap
them if they are in wrong order.
3. Proceed till the last element of the list in a similar fashion.
4. Repeat all of the above steps until the list is sorted.
1. Bubble Sort-