List Manipulation Code + Outputs
List Manipulation Code + Outputs
, .m\\''
WORKSHEET - UST MANIPULATION
\\'hat ,rill \JC' 1he output or lnllowi 11 1{ codt.'
listl c{l 1 3, 21
list\ t 2
~
.2 ~ Whnt~1ll be the output of following code-
l="Welcome to Python".split()
I 1
t
2
I
I
:3 · What will be the output of following code-
print([i.lowerl) for i in "HELLO"])
I
/4 I What will be the output of following code-
=m
a 1,2J,[3,4J,sJ,[6, 7JD
ry- I I a[0][l][l]
i
I
I IiWh:,t will be the output of following code-
/5
a,b-[3, 1,2),(5,4,6)
' a+b
I
I
I
6 What wtll N the oatpat of following program:
•=(2, 1,3,5,2,4}
a.remow(2)
•
!
-- --- -- .
11
,,
- ~ - ~ ~
r ~otu1·
1~7 I Ex1>lnt11 Li·st.. Com1>rnlrnnslo11 ~ new List. ---111•
l
I
.. . .
nnd .
Efogunt way to ~-
create . •
,-R
8 i What will be the output of following program:
theList=[l, 2, [1.1, 2.21]
len(theList)
9 I What will be the output of following program:
my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']
print('p' in my_list)
print('a' in my_list)
print('c' not in my_list)
I
10 I What will be the output of fo.
for fruJt Jn ['apple', 'banana•;
print("I like",fnut)
l/
t ......
• ~,1,\1\,~:t\O~
l1
W.hnt wm b~ the output of fc-:, .lh•wlng program:
J>Ow2 • 12 ~« x fo.1· x .In .mngc( 10) lf x > 61
:1>ow2
1.2 What will be tht~ output of following program:
odd • l[x for x in rnnge(20) if x '¼, 2 - " 1)
odd
13 What \\·ill be -the output of following program:
l=[.x+y for x in ('Python ','C '] for yin ['Language','Programming'])
I
1~ What will be the output of following program:
odd= [1, 9]
odd.insert(l,3)
print(odd)
odd[2:2] = [5, 7]
print(odd)
15 What will be the output of following program:
odd= [1, 3, SJ
print(odd + [9, 7, SJ)
print(["re"J * 3)
16
What will be the outnut nt 'oDowing
odd = [2, 4, 6, BJ -r-•..-••• ----·-- - - --..
odd[OJ =l
L.._ print(oddJ
3/ P2ge
~~-- - ~
Qch\\l t41 • \3, 6, 7\
\l rl n t{ o d {l}
---==--- - - - - - - ------'
l r' Whtit. will be the out.put: of fo llowi n g p rogram :
listl • {"1,ython", ''list", 1952, 2323, 4 32]
list2 • ("this", "ls", ''another", "list"]
'p rint {list: 1)
print{listl ll :41)
printl\ist 1{ 1:1)
print{list l[Ol)
printllistl * 2)
print{listl + list2)
18 What will be the output of following program:
C\.. 1=[10,20,30,40,50,60]
for i in range(l,6):
l[i-l]=l[i]
for i in range(0,6):
1 --;=-
/ 03 I print(l[iJ,end=")
~ I
1 9 I What will be the output of following program:
1=[6, 12, 18,24,30]
f\.. I for i in J:
~
for j in range(l,i%5):
printU, '#' ,end=")
print()
IIIIIIC ...._
-..,,. ... .. - -
._
<:a.a
'b Whl\\ wm be tho outvut of fcJllowlng progrorn:
dntu • \\\ 11 2\ 1 \3, 4 \), 116, ,11, 17, 8111
\lrlnt{dntn\ 1\\0}\0))
I
_____ _J
I
\ .'.' Whnt. wm be the output of following program:
a •"hcllo"
b =Ust((x.uppcr(),lcn(x)) for x in a)
prlnt{b}
2S ~What will b; the output of following program:
a={OJ*3
a(l}.append(7)
print(a))
29 ~ will be the output of following program:
Ll = list()
Ll.append([l, [2, 3), 41)
Ll.extend([7, 8, 91)
1
print(Ll[OJ[l][l] + L1[2])
f3U f What wm be the output of following program:
/List= {True, 50, 10)
List.1nsert(2, 5)
pr1nt(L11t, "Sum fa: ", 1um(Ll1t))
31 What will be the output of following program:
,, 7, SJ
,end=""
6/PaH
,:-~
T Whnt wltl lnl t h•l ,,utput of followlot( progrum :
120 unmc8 • rnonllk' ·v 'trnl.', ·.1rnt111I', ,,,, ..,v,ui'l
1
1,rlnt(nntnt)Bl• ,l u-.111
--- ------------
What will b<l th" output of foJlowlnll proll'"""
21 "Wdcomc to Python4csip.coPl"••PUt()
22 f\\lbat will be the output of following progrll,;i.:
rnyList = [1, 5, 5, 5, 5, 1)
max = rnyList[O]
indexofMax = O
for i in range(l, len(rnyList)):
if rnyList[i] > rnax:
rnax = rnyList[i]
· indexOfMa.7' = i
f print(indexOfMa.7')
I
I
I
What will be the output of the program?
[23 x + 1, x + 2J for x in range(O, 3)1
,n == {[x,
m
What will be the output of following program:
24
Ust("a #b#clld". split('#'))
fiat wfJJ 1,e die oatpllt of . . .•1111
nlu• = aa, 4, 5, 1 J, (33, 6, 1, 2JI
tor '"1 bl nlaea:
----•t,
.....arttJ
,_.,....tmror.
ead = • •1
;!~~
V
.
I',
..,,.
~--
...,..
What WUJ be t h ~ o uq,ut o f fbllowln g progrum :
A • [~t 4, 6, 8,10)
L • lt"n (A)
S• o
for l in :r a n ge (1, l,, 2 ):
S-t • A-tl]
I'
Prin t ("Su n1•'' !S) I
I
I
I
3.S-
Given a Python list, find value 20 in the list, and if it is present, replace it with
200. Only update the first occurrence of a value
listl = (5 , 10, 15, 20, 25, SO, 20)
Expectedoutput:
list 1 = [5 , 10, 15, 200, 25, 50, 20)
39 Which function is used to reverse objects of list In place.
I
1----.1-- - - -· -
140 IWrite a Python program to sum all the itema la a JIIII:.
II
I
I
I
I
.,
222lii±E!IW_
~ --,,---.,
f4
11rh,t {~11~\T\6}-2)-4])
Who\ will bO the ()ntpul uf toUowh1g progrprn:
...~ \
1al.ist • \4, S, 12, 16}
inList\1:4] • [20, 24, 28)
·p rint(nUst)
33 ~
-+- What
.- _·will
- ~
be the output of following prograrn:
1 = {None] * 10
print(len(ll}
What will be the output of following program:
3~ sampleList = [10, 20, 30, 40, 50)
sam pleList. pop()
print(sampleList)
sampleList.pop(2)
print(sampleList)
I
35 I!What will be the output of following program: •· I
resLlst = (x+y for X in ['Hello I' 'Good '] for ., in rDear', 'BJe'D
printfresList)
t di .. tlae
•-. •-- a
., ...
l" llJn,,.
il 4 l Write I program to 1hlf't every element of a ll1t to circularly rtpt. £,&,-
INPUT : 1 2 3 4 5
OUTPUT : 5 1 2 3 4
4
5 i Take a list of 10 elements. Split It into middle and store th• eJemeata ID two
\ dfferent lists. E.g.-
\ INITIAL list :
63 9 8 81 1
,,.
58 24 13 15
I After spliting : 15 63
I 58 24 13
81
78
9 8
146 IP)'thon PrognDl to Find the e m•
------ .,i
Write 11 Python progrnm to gel: the Jorgcat number from a 11st,
n
42 Write a Python program to count the number of strings .vhere the string length
is 2 or more and the first and last character are same from a given list of
strings.
Sample List: ['abc', 'xyz', 'cbc', '121')
Expected Result : 2
, 43 I Write a Python program to re1110ff duplicates rro111 a u.t.
I
~
l=\10,20 ,30,40,50 ,60\
for i in rangc\lcn\ll):
U\i''i~,2=r: 0):
prlntl1\l},cnd11:' #')
else:
pr\ntll\i\)
~--i What will be the output of the following program:
, 4S t
l=ll0,20,30,40,50,601
for i in range(len(l)):
if\i%2==0):
print(l[i],end='#')
else:
print(l[i],end='@J')
49 Program to Find Even elements in a List:
I
i I
I
I
I I
I I
I
I
50 I J'loplll to Prlat Jlat haviDC numben le.. thaJl 10
Au:
JJ/
~ 'fietw 1
I
I I
I
\
I --------1
I
\ 51
\
I What will be the output of following program:
l=[[i*j for j in range(l,11)] for i in range(7,9)]
\1
\
\ _________________,
, 52 \ W~ to display the frequency of each item In a list.
I
\
I
I
f 53 Twhat will be the output of the following program:
/ I 1=(6, 12, 18,24,30)
, I for i in I:
/ for j in range( 1,1%4):
printU, '#',end=")
print()