0% found this document useful (0 votes)
13 views53 pages

List Chapter

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views53 pages

List Chapter

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 53

IP CLASS –XI

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

How to take input Numbers in list


Program2
L=eval(input("enter the list elements"))
print(L)
output
[2, 3, 4, 5, 6, 7]
DATE: 16 SEP 2020

Write a program to find the sum of 5 subject marks


entered in a list by user. Also find the percentage and
division .

40 50 50 30 12
0 1 2 3 4

L=eval(input("Enter the marks of your five Subjects"))


sum1=0
for i in range(len(L)):
sum1=sum1+L[i]
print(sum1)
EXPLAINATION
I L[i] I< len(L) (i.e. 5) Sum=sum+i
0 40 Y 40
1 50 Y 90
2 50 Y 140
3 30 Y 170
4 12 Y 182
5 F stop
Assignment # 31
Date: 16 SEP 2020

Chapter‘s Name: STRING

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

Write a program to find the sum of 5 subject marks


entered in a list by user. Also find the percentage and
division .

40 50 50 30 12
0 1 2 3 4

L=eval(input("Enter the marks of your five Subjects"))


sum1=0
for i in range(len(L)):
sum1=sum1+L[i]
print("Sum:",sum1, "out of 500")
per=sum1/500*100
print("Percentage:", per)
# it is used to find the division
if per>=60:
print("First Div")
elif per>=48:
print("Second Div")
elif per>=33:
print("Third Div")
else:
print("Fail")
OUTPUT:
Enter the marks of your five Subjects[40,50,50,30,12]
Sum: 182 out of 500
Percentage: 36.4
Third Div
ACCESSING LIST:
ACCESS OR MODIFY OR VIEW THE ELEMENT OF A LIST .

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

Chapter‘s Name: LIST MANUPULATION

1.What is a list access?


2.Write any two similarities between strings an list.
3.Write a program to find the sum of even numbers in
a list. Take 10 numbers first as input in a list from
the user.

4. Write a program to count the vowels from the


following list.
List=[‘a’, ’f’, ’o’, ’y’, ’u’, ’m’, ’p’, ’e’]

5. Write a program to count the character ‘a’ in the


list of names given below:
Names=[‘Raj’, ’Rishi’, ‘Mukesh’, ‘amit’,’rahul’]
18.09.2020
membership operator (in and not in)

in operator : it is returning boolean value true or


false according to the situations
returns true when element is a part of list.

L=[10,20,30,40]
>>> 30 in L
True

Not in : booleav value (True or False) , return true


when the element is not a part of list

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

Chapter‘s Name: LIST MANUPULATION

1.Given a list V=[1,2,4,6,8,16]. Give output of


following commands
a) print(V[-3]
b) print (V[2])
c) print(4 in V)
d) for i in V:
print(i)
e) print(5 in [1,2,4,6,8,16])
f) print(V*2)
2. what is list concatenation operator. Give example
19.9.2020
Traversal in a list
Access the elements in a list from one end to another
one by one .it is process of visiting each element of a
list and doing some task
Example of traversal.
L = [1,3,4,5]
for I in range(len(L)):
print(L[i])

-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

Chapter‘s Name: LIST MANUPULATION

1. What do you mean by list traversal? Give an example to illustrate


this in list
2. Is traversal is possible in strings.(T/F)
3. Find output:
L=[1,2,3,4,5,6,7]
for i in range(len(L)):
Print(L[I-len(L)])
4. What do you mean by comparing list.
5. Find output:
>>> L1=[1,2,3]
>>> L2=[4,5,6]
>>> L3=[4,5,6]
>>> print (len(L1)==len(L2))
>>> print (L1==L2)
>>> print (L1==L3)
21.9.2020
comparing list
Two list are said to be equal when each of their
corresponding elements are same.

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

Chapter‘s Name: LIST MANUPULATION

1. Name the type of list operations.


2. Compare the following and give result

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

REPLICATION OPERATOR (*) : Repeat of list elements

L=[1,3,6]
>>> L*2
[1, 3, 6, 1, 3, 6]
>>> 2*L
[1, 3, 6, 1, 3, 6]

SLICING OF A LIST : CUTTING INTO PIECES

FOR A SEQUENCE, WE CAN APPLY A SLICING AS FOLLOWS

Seq = L [start: stop: step]


Seq= the output list
Start = starting index for the list. (default : zero)
Stop= STOP -1 (default : end index len(l) -1 or highest index)
Step = diff or gap .between the two member (default : +1)
Example
>>> L=[10,12,14,20,22,24,30,32,34]
>>> s=L [3:7:1] # slicing (output of it is list)
>>> print(s)
[20, 22, 24, 30]
Slicing. There are three values
String[ start : stop : step]
Start : from which index u want to start
Stop : 1 less than where you want to stop.
Step : by what element you want to move
(Step For Moving ) step can be positive negative.
STEP
Default values :+1
Positive Negative
direction of left to right right to left
move
start ,stop ,ste if start<stop if start >stop
p ok ok
else else
blank LIST blank LIST will be
will be generated
generated
default start 0 length -1 or last s="indian"
value index >>> s[ :4:1]
default stop last index 0 s="indian"
value s[::2]
default step +1 - s="indian"
value s[1:5]
IP
Assignment # 36
Date: 22 SEP 2020

Chapter‘s Name: LIST MANUPULATION

1. What is the list replication operator?


2. Compare the following and give result

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

Example Of List Slicing

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

Chapter‘s Name: LIST MANUPULATION

1. What is the list slicing?


2. Compare the following and give result
L = [10,12,14,20,22,24,30,32,34]

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

Chapter‘s Name: LIST MANUPULATION

1. What is the list slicing?


2. Compare the following and give result
L = [10,12,14,20,22,24,30,32,34]

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

Program to find minimum element from the list.

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

#program to find minimum element in a list (TB PN 204


PROGRAM NO 6.7)
L=eval(input("enter in the list"))
min=L[0]
pos=0
for i in range(1,len(L)):
if L[i]< min:
pos=i
min=L[i]
print("min element is at index @", pos,"with min value",min)

Output:
enter in the list[60,75,73,61,56,78,98]
min element is at index @ 4 with min value 56

#program to find maximum element in a list


L=eval(input("enter in the list"))
max=L[0]
pos=0
for i in range(1,len(L)):
if L[i]> max:
pos=i
max=L[i]
print("max element is at index @", pos,"with max value",max)
output
enter in the list[10,20,30,40,50,60,70,80,90]
max element is at index @ 8 with max value 90

26.09.2020
PROGRAM 6.8 (TB PNO 205)

MEAN OF ALL THE NUMBER

 TAKE INPUT A LIST


 SUM OF ALL THE NUMBER IN A LIST
 LENGTH OF LIST
 DIVIDE SUM BY LENGTH

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

Chapter‘s Name: LIST MANUPULATION

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(),

Srno Function Return desc Example


name type
1 Len( ) int Returns >>> L=[1,4,5]
the >>> len(L)
length( no 3
of >>> L=[1,4,[5,6]]
element >>> len(L)
or items ) 3
in a list
2 List() List Returns a >>> L=list((34,36,79))
list ot the >>> print(L)
elements [34, 36, 79]
passed to >>> S=list("india")
it >>> print(S)
['i', 'n', 'd', 'i', 'a']
>>> b=list()
>>> print(b)
[]
3 Index() int Returns L=[10,20,30,40,50,60,70]
the index >>> L.index(40)
of the 3
element >>> S=["india",
specified "china","pak", "usa"]
>>> S.index("usa")
3
>>> S.index("pak")
2
>>> S.index("Srilanka")
Traceback (most recent call
last):
File "<pyshell#22>", line 1,
in <module>
S.index("Srilanka")
ValueError: 'Srilanka' is not
in list
4 Append( None Adds an L=[1,3,5]
) item to >>> L.append(7)
the end of >>> print(L)
the [1, 3, 5, 7]
existing >>> L.append([6,8])
list >>> print(L)
[1, 3, 5, 7, [6, 8]]
>>> len(L)
5
>>> L.append("Hello")
>>> print(L)
[1, 3, 5, 7, [6, 8], 'Hello']
5 extend() None
IP
Assignment #40
Date: 28 SEP 2020

Chapter‘s Name: LIST MANUPULATION

Solved Questions (210-214)


Q no : 8,
Ncert questions
Pno 215 Qno 5,6,7

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

Chapter‘s Name: LIST MANUPULATION

1. What is the difference between the insert and append method.


2. TB QUESTION Solved Questions (PNO : 210-214) Q No : 9,
3. TBPage no : (217-220 ): TYPE A: 9,10,11
30.09.2020
LIST FUNCTIONS

6 Insert None Add the SYNTAX


element at a LIST.INSERT(<POS>, <VALUE>)
particular
index or L.insert(2,50)
position >>> print(L)
[1, 2, 50, 3, 10, [11, 12, 13], 11,
12, 13]
7 Pop() Int Remove the >>> L=[10,20,30,40,50,60,70]
item whose >>> L.pop(2)
index is 30
given and >>> print(L)
return the [10, 20, 40, 50, 60, 70]
item >>> L.pop(0)
removed. 10
>>> print(L)
[20, 40, 50, 60, 70]
>>> L.pop()
70
8 Remove() Remove the >>>L=[20,40,50,60]
item given >>>L.remove(50)
the function >>> print(L)
[20, 40, 60]

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

13 SORTED No RETURNS A >>> L=[10,15,7,2,11,20,9]


SORTED >>> S=sorted(L)
LIST >>> print(S)
[2, 7, 9, 10, 11, 15, 20]
Difference between sort and sorted on page no 388 table 11.1
14 Min() >>> min(L)
2
15 Max() >>> max(L)
20
16 Sum() >>> sum(L)
74
LIST CONTAINS LIST AS ELEMENTS : NESTED LIST
LA=[22,11]
LB=[33,11]
LX=[11,LA,LB,15]
LX HAS LA ,LB AS ELEMENTS WHICH ARE LIST

Deleting a sublist from the list

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

Chapter‘s Name: LIST MANUPULATION

TYPE A ON PAGE NO-218 TBQ 12,15,17

TYPE B ON PAGE NO –218 -220 TBQ 7,8,9,13,15

CLASS TEST ON 03 OCT 2020 –FLOW OF CONTROL CHAPTER

You might also like