List Chapter
List Chapter
LIST MANIPULATION
LIST:
Collection of data which may or may not be of same
type.IT IS MUTABLE SEQUENCE IN WHICH ALL THE
ELEMENTS CANBE MODIFIED. IT IS COLLECTION IN
SQURARE BRACKETS.
Data
a) Characters ‘a’, ’e’, ’I’, ’o’ , ‘A’,‘E’,’O’,’I’, ‘U’
b) Integer 0,2,4,6,8 10
c) FLOAT Num 0.5 ,1,1.5,2.0
d) Strings “Raj”, ”Ravi”, ”Ram”
EXAMPLE OF List:
List of vowel [ ]
V= [‘a’, ’e’, ’I’, ’o’]
I= [0,2, 4, 6,8 10]
F=[ 0.5 ,1,1.5,2.0]
FRD= [“Raj”, ”Ravi”, ”Ram”]
L = [10,’RAM’, 56.5]
“HELLO”
SEQUENCES-STRINGS, LIST AND TUPLE
Simmilarity
DIFF BETWEEN STRING AND LIST
STRING LIST
Collection of character It is collection of item which
enclosed in may or may not of same
single ,double or triple type (Heterogenous)
quotes
All the element are Elements are character ,
character number or string, list
Immutable Mutable
EMPTY LIST
L=[ ]
NESTED LIST: LIST AS A ELEMENT INSIDE ANOTHER LIST.
L=[1,2,3,[4,5,6]]
INDEX:
EVERY ELEMENT IS GIVEN A NUMRIC POSITION IN A
START STARTs FROM 0 TO L-1, where L is length of the
list(i.e.no of items or elements in the list)
L = [10,20,30,40,50]
LENGTH=5 INDEX = 0 TO 4 (LENGTH -1)
INDEX IS USED TO ACCESS OR MODIFY ELEMENTS IN
LIST.
Eg print(L[1])
Positive index
Start from left to right i.e . start from 0 to N-1, Where N
is length of the list
10 20 30 4 50
0
0 1 2 3 4
10 20 30 4 50
0
0 1 2 3 4
Negative Index:
Starts from right to left from -1 to –L , where L =length
of the list.
10 20 30 4 50
0
-5 -4 -3 -2 -1
Nested List
L = [10, 20, [24,27,29], 30]
-2
-4 -3 -1
-3 -2 -1
10 20 24 27 29 30
0 1 2
0 1 2 3
EXAMPLE
L=[10,20,[24,27,29],30]
>>> print(L)
[10, 20, [24, 27, 29], 30]
>>> len(L)
4
>>> print(L[1])
20
>>> print(L[0])
10
>>> print(L[2])
[24, 27, 29]
>>> print(L[2][0])
24
>>> print(L[2][2])
29
>>> print(L[-1])
30
>>> print(L[-2][-2])
27
CREAT E LIST FROM EXISTING SEQUENCES
L=list("hello")
>>> L
['h', 'e', 'l', 'l', 'o']
>>> t=('h','e','l','l','o')
>>> T=list(t)
>>> T
['h', 'e', 'l', 'l', 'o']
Creating list by taking element input from user
Program 1
L=list(input("enter the list elements"))
print(L)
output:
enter the list elements234567
['2', '3', '4', '5', '6', '7']
40 50 50 30 12
0 1 2 3 4
1.What is a list?
2.What is the difference between the list and String
3.Is the list are mutable or immutable True or False
4.How can you create an empty list
5.What is a nested list. Give an example
6.Write a code to create a list of character from the
string “ambuja”
7.Write a code to create the list of numbers enter by
the user
8.Write a program to enter the runs scored by a
batsman in 5 innings in a list . find the sum and
average of the runs made [use list to store runs of
batsman]
DATE: 17 SEP 2020
40 50 50 30 12
0 1 2 3 4
L=[1,2,3,4,5]
>>> print(L[2])
3
>>> L[1]=10
>>> print(L)
[1, 10, 3, 4, 5]
NOTE
WE CAN USE BOTH FORWARD AND BACKING INDEX
FOR ACCESSING ELEMENTS
BEACUASE OF ACCESSING, WE CAN MODIFY THE
ELEMENT’S VALUE BECAUSE LIST IS MUATABLE .
THIS IS NOT POSSIBLE IN STRING BECAUSE STRING IS
IMMUATABLE.
SIMMILARITY BETWEEN STRING AND LIST
BOTH LIST AND STRING ARE SEQUENCES
1.LENGTH . USE len() to find length (i.e. no of
characters in a string or no of items in alist)
2.Index : both list and string have forward and
backward indexing
3. Slicing : both list and string follow slicing concept
4.Both follow the membership operator (in and not
in)
5.Concatenation operator (+)
6.Replication operator(*)
Assignment # 32
Date: 17 SEP 2020
L=[10,20,30,40]
>>> 30 in L
True
Concatenation operator(+)
l1=[1,2,3]
>>> l2=[4,5,6]
>>> l=l1+l2
>>> print(l)
[1, 2, 3, 4, 5, 6]
Replication operator
L1=[1, 2, 3]
>>> print(L1*3)
[1, 2, 3, 1, 2, 3, 1, 2, 3]
Assignment # 33
Date: 18 SEP 2020
-5 -4 -3 -2 -1
40 50 50 30 12
0 1 2 3 4
How to find negative index from positive index
a-Len(L) where a is positive index
element pos-ind negate index =0
first 0 0 -5= -5
second 1 1 - 5=-4
comparing list
Two list are said to be equal when each of their
corresponding elements are same.
Assignment # 34
Date: 19 SEP 2020
>>> L1 = [1,2,3]
>>> L2 = [4,5,6]
>>> print(L1==L2)
False
FOR >, <,>=,>=.,<=
>>> [1,2,8,9]<[9,1]
True
>>> [1,2,8,9]<[1,2,9,1]
True
>>> [1,2,10,9]<[1,2,5,1]
False
example
>>> a=[2,3]
>>> b=[2,3]
>>> c=['2','3']
>>> d=[2.0,3.0]
>>> e=[2,3,4]
>>> a==b
True
>>> a==c
False
>>> a>b
False
>>> d>a
False
>>> d==a
True
>>> a<e
True
>>>
List Operations:
Joining list
a) Using + operator
Example
>>> l=[1,2,3]
>>> m=[6,7,8]
>>> l+m
[1, 2, 3, 6, 7, 8]
>>> n=['1','2']
>>> l+n
[1, 2, 3, '1', '2']
>>> k=[2.0, 3.0]
>>> k+l
[2.0, 3.0, 1, 2, 3]
>>> r=['raj','ram']
>>> k+r
[2.0, 3.0, 'raj', 'ram']
>>> l+r
[1, 2, 3, 'raj', 'ram']
>>> r+n
['raj', 'ram', '1', '2']
>>> j=[1,2,[3,4]]
>>> l+j
[1, 2, 3, 1, 2, [3, 4]]
Using + operator , we can two lists only
It is invalid if the other operand is
a) Number eg l+5 is wrong
b) String eg. L+’hello’ is wrong
List with += opearator
m=[1,3,5]
>>> l+=m
>>> l
[1, 3, 4, 'a', 'b', 'c', 1, 3, 5]
>>> a=10
>>> b=5
>>> a+=b
>>> print(a)
15
IP
Assignment # 35
Date: 21 SEP 2020
Output
>>> [1,5,8] > [5,9,15]
>>> [1,3,5] > [1.0,3.0.5.0]
>>> [1,3,5] == [1.0,3.0,5.0]
22.09.2020
List Operations
L=[1,3,6]
>>> L*2
[1, 3, 6, 1, 3, 6]
>>> 2*L
[1, 3, 6, 1, 3, 6]
Output
>>> [1,5,8]*2
>>> 3*[1,3,5]
>>> 2*[1,3,[5,7] ]
print(L[4:8])
start= 4
stop= 8 -1 =7
step = if nothing is there, default +1 , we should take
L = [10,12,14,20,22,24,30,32,34]
>>> S=L [3:7:1]
>>> print(S)
[20, 22, 24, 30]
>>> print(L[3:7:1])
[20, 22, 24, 30]
>>> print(L[2:8:2])
[14, 22, 30]
>>> print(L[4:8])
[22, 24, 30, 32]
>>> print(L[2:8:3])
[14, 24]
>>> print(L[3:-2:+1])
[20, 22, 24, 30]
IP
Assignment # 37
Date: 23 SEP 2020
Output
>>> L[3:8:1] [ 20,22,24,30,32]
>>> L[2:8:3] [ 14,24]
>>> L[2:8:2] [14,22,30]
>>> L[1:7] [12, 14, 20, 22, 24, 30]
>>> L[1:8] [12,14,20,22,24,30,32,
23.09.2020
EXPLAINED SLICING WITH VARIOUS EXAMPLW
L = [10,12,14,20,22,24,30,32,34]
>>> L[2:8:3]
[14, 24]
>>> L[1:7]
[12, 14, 20, 22, 24, 30]
>>> print(L[-1: -5:-1])
[34, 32, 30, 24]
>>> print(L[-1: -5:1])
[]
>>> print(L[4:-3])
[22, 24]
>>> print(L[:7])
[10, 12, 14, 20, 22, 24, 30]
>>> print(L[::1])
[10, 12, 14, 20, 22, 24, 30, 32, 34]
>>> print[::+2]
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
print[::+2]
TypeError: 'builtin_function_or_method' object is not subscriptable
>>> print(L[::2])
[10, 14, 22, 30, 34]
>>> print(L[::])
[10, 12, 14, 20, 22, 24, 30, 32, 34]
>>> print(L[-6:-2:1])
[20, 22, 24, 30]
>>> print(L[-6:-2:-1])
[]
IP
Assignment # 38
Date: 23 SEP 2020
Output
>>> L[3:8:1] [ 20,22,24,30,32]
>>> L[2:8:3] [ 14,24]
>>> L[2:8:2] [14,22,30]
>>> L[1:7] [12, 14, 20, 22, 24, 30]
>>> L[1:8] [12,14,20,22,24,30,32,
25.09.2020
56 30 44 21 55 27 90 87 95 45
0 1 2 3 4 5 6 7 8 9
Min=L[0]=56
Traversal list
I =1 to last index
I L[i] L[i]<min MIN
1 30 min =L[I] L[3]
2
3
4
Output:
enter in the list[60,75,73,61,56,78,98]
min element is at index @ 4 with min value 56
26.09.2020
PROGRAM 6.8 (TB PNO 205)
SEARCH IN LIST
#search in a list
L=eval(input("enter in the list"))
print(L)
N=int(input("enter the number"))
for i in range(len(L)):
if L[i]==N:
print("found at index",i)
break
else:
print("Not found")
IP
Assignment # 39
Date: 26 SEP 2020
Solved Questions
Pno 209 q no 2,3
Pno 210 q no 5,7
Ncert questions
Pno 215 qno 3, 4
List Manupulation
28 SEP 2020
List function : Those which are applied on a list and returns results.
Eg. Len(),
PN 0 217-220
TYPE A 3,4,5
TYPE B 1, 2,4
29.9.2020
List functions (Contd)
5 extend() None Used for t=('a','b','c')
adding >>> print(t)
multiple ('a', 'b', 'c')
elements >>> L.extend(t)
given in >>> print(L)
the form [1, 3, 5, 7, [6, 8], 'Hello', 'a',
of list or 'b', 'c']
tuple n=[10,11,12]
>>> L.extend(n)
>>> print(L)
[1, 3, 5, 7, [6, 8], 'Hello', 'a',
'b', 'c', 10, 11, 12]
>>> L.append(n)
>>> print(n)
[10, 11, 12]
>>> print(L)
[1, 3, 5, 7, [6, 8], 'Hello', 'a',
'b', 'c', 10, 11, 12, [10, 11, 12]]
6 Insert None Add the
element
at a
particular
index or
position
IP
Assignment #41
Date: 29 SEP 2020
>>> L.remove(100)
Traceback (most recent call
last):
File "<pyshell#20>", line 1, in
<module>
L.remove(100)
ValueError: list.remove(x): x
not in list
9 Clear() None Clear all the >>> L.clear()
list >>> print(L)
[]
10 COUNT() INT COUNT A >>>
PARTICULAR L=[10,10,20,20,30,20,40,50]
ITEM >>> L.count(30)
1
>>> L.count(20)
3
>>> L.count(10)
2
11 Reverse NONE Reverse the >>>L=[10,10,20,20,30,20,40,50]
list in place >>> L.reverse()
>>> print(L)
[50, 40, 20, 30, 20, 20, 10, 10]
12 Sort None Asc or desc >>> print(L)
arrange the [50, 40, 20, 30, 20, 20, 10, 10]
list >>> L.sort()
>>> print(L)
[10, 10, 20, 20, 20, 30, 40, 50]
DESC
>>> M=[5,10,5,20,15,2]
>>> M.sort(reverse=True)
>>> print(M)
[20, 15, 10, 5, 5, 2]
01.10.2020
FUNCTIONS
Del listname[index]
Or
Del list [start:stop]
Example
>>> L=[10,15,7,2,11,20,9]
>>> del L[1]
>>> print(L)
[10, 7, 2, 11, 20, 9]
>>> del L[3:6]
>>> print(L)
[10, 7, 2]
IP
Assignment # 42
Date: 01 oct 2020