List manipulation
'''1.WAP TO PRINT LIST OF ELEMENTS WITH INDEX
VALUES'''
lst=list(eval(input("enter the list")))
leng=len(lst)
for i in range(leng):
print("for inedx values",i,'and',i-
leng,'the value is',lst[i])
OUTPUT:
enter the list[1,2,3]
for index values 0 and -3 the value is 1
for index values 1 and -2 the value is 2
for index values 2 and -1 the value is 3
'''2 wap to traverse a list'''
lst=list(eval(input("enter the list-")))
for i in lst:
print(i)
enter the list-[1,2,3]
1
2
3
3.'''extract 2 list of a given list ofno's adn
display and print sum of elements of 1st list slice
which contsins which contains every other element
btw 5 to 15 prog should asalso displa yavf of 2nd
slice which contains every 4th element'''
lst=[10,20,30,40,1,2,3,100,200,10,20,30,11,12,15,11
,90,77,35]
lst1=lst[5:15:2]
lst2=lst[::4]
sm1=sm2=0
for i in lst1:
sm1+=i
for i in lst2:
sm2+=i
print("sum of lst1=",sm1)
print("sum of lst2=",sm2/(len(lst2)))
OUTPUT:
sum of lst1= 154
sum of lst2= 62.4
4.'''wap to print list elements aznd find sum of +
elements'''
lst=list(eval(input("enter the list-")))
sm=0
for i in lst:
if i>0:
sm+=i
print(sm)
OUTPUT:
enter the list-[1,2,3,4]
10
5.'''input list and 2no's m and n crt list from
those elements which are divisible by m and n '''
lst=list(eval(input("enter the list-")))
m=int(input("enter the no-"))
n=int(input("enter n-"))
final=[]
for i in lst:
if m%i==0 and n%i==0:
j=list(str(i))
final+=j
print(final)
OUTPUT:
enter the list-[1,81,40,20]
enter the no-2
enter n-3
['1']
6.'''wap to find highest and lowest int in given
list'''
#wap to find largest and smallest no in the list
lst=list(eval(input("enter the list-")))
mn=0
mx=lst[0]
for i in lst:
if i<=mx and i>mn:
mn=i
if i>=mx:
mx=i
print([mn,mx])
OUTPUT:
enter the list-[2,9,5,1]
[1, 9]
7.'''given 2 lists wap to check if it has common
element '''
lst1=list(eval(input("enter the list-")))
lst2=list(eval(input("enter the 2nd list-")))
index=len(lst1)
index1=len(lst2)
for i in lst1:
for j in lst2:
if i==j:
result=True
print('overlap')
break
OUTPUT:
enter the list-[1,2,3]
enter the 2nd list-[3,4,5]
overlap
8.''wap to find 2nd largest value in a given list
''
#wap to find largest and smallest no in the list
lst=list(eval(input("enter the list-")))
mn=0
mx=lst[0]
i=0
while i<2:
mn=0
mx=lst[0]
for j in lst:
if j <= mx and j > mn:
mn = j#1
if j > mx:
mx = j#4
lst.remove(mx)
lst.remove(mn)
i+=1
print([mn,mx])
OUTPUT:
enter the list-[1,2,3,4,5,6,7,8]
[2, 7]
9.WAP TO INPUT LIST AND ELEMENT AND REMOVE ALL
OCCURENCES OF GIVEN ELEMENT
#wap to take a list and element and remove all
occurences of it
lst1=list(eval(input('enter the list-')))
lst1.sort()
element=int(input('enter the element'))
while element in lst1:
lst1.remove(element)
print(lst1)
OUTPUT:
enter the list-[1,2,2,3]
enter the element2
[1, 3]
10.WAP TO TAKE A LIST AND SHIFT ALL 0’S TO RIGHT
AND OTHERS TO THE LEFT
#wap to input a list and sort it in acsending order
lst=list(eval(input("enter the list elements-")))
lst.sort(reverse=True)
print("new sorted list is-",lst)
OUTPUT:
enter the list elements-[1,2,34,91,0,2,0,3]
new sorted list is- [91, 34, 3, 2, 2, 1, 0, 0]
11. WAP TO PRINT A PROGRAM THT TAKES 2 LISTS AND
CREATES WITH LISTS OF ELEMENTS ALTERNATING FROM THE
1ST AND 2ND LIST
#wap to take 2 tuples and create a new tuple with
alternating elements
tuple1=list(eval(input('enter the 1st tuple-')))
tuple2=list(eval(input('enter the 2nd tuple-')))
tuple3=[]
while True:
tuple3.append(tuple1.pop(0))
tuple3.append((tuple2.pop(0)))
if len(tuple1)==0 or len(tuple2)==0:
print('yes')
break
print(tuple(tuple3))
OUTPUT:
enter the 2nd tuple-[4,5,6]
yes
(1, 4, 3, 5, 2, 6)
'''12.wap to inpt list and find no's are
armstrong"'''
lst=list(eval(input("enter the list-")))
mn=0
mx=lst[0]
for i in lst:
sm=0
n=i
while i>0:
dig=i%10
cube=dig**3
sm+=cube
i=i//10
if n==sm:
print(n)
if i>mn:
mx=i
if i<=mn:
mn=i
print(mx,mn)
OUTPUT:
enter the list-[371,153,1,0]
371
153
1
0
371 0
13. WAP THT ASKS USER TO INPUT A NO OR A LIST AND
APPEND IT TO AN EXISTING LIST.
'''WAP THT ASKS USER TO INPUT A NO OR A LIST AND
APPEND IT TO AN EXISTING LIST.'''
lst1=list(eval(input('enter list or an item ')))
lst=list(eval(input('enter the main list-')))
if len(lst1)==1:
lst.append(lst1)
else:
lst.extend(lst1)
OUTPUT:
enter list or an item [1,2,3]
enter the main list-[1,2,3]
[1, 2, 3, 1, 2, 3]
14. WAP THT DISPLAYS OPTIONS TO DELETE ELEMENTS
FORM A LIST AND IF ITS TO BE DONE IN A SLICE MANNER
OR THE LAST ELEMENT OR ANY INDEX SPECIFIED VALUE OR
THE SPECIFIC VALUE OF ELEMENT
import time
'''WAP THT DISPLAYS OPTIONS TO DELETE ELEMENTS FORM
A LIST AND IF ITS TO BE DONE IN A SLICE MANNER OR
THE LAST ELEMENT OR ANY INDEX SPECIFIED VALUE OR
THE SPECIFIC VALUE OF ELEMENT '''
lst=[1,2,3,4,5,6,7,8,9]
print('The given list is ',lst)
time.sleep(1)
print('how do u want to delete ur list-')
print('if last element is to be deleted press 1')
print('if u want to delete specific value press 2')
print('if u want to delete values using index
positions press 3 ')
print('if u want to delete slice press 4 ')
choice=int(input('enter ur choice-'))
if choice==1:
lst.pop()
elif choice==2:
el=input('enter the element-')
lst.remove(el)
elif choice==3:
ch=int(input('enter index value'))
lst.pop(ch)
elif choice==4:
ll=int(input('lower llimit='))
ul=int(input('upper limit='))
for i in lst[ll:ul]:
lst.remove(i)
else:
print(' invalid ')
print(lst)
OUTPUT:
The given list is [1, 2, 3, 4, 5, 6, 7, 8, 9]
how do u want to delete ur list-
if last element is to be deleted press 1
if u want to delete specific value press 2
if u want to delete values using index positions
press 3
if u want to delete slice press 4
enter ur choice-1
[1, 2, 3, 4, 5, 6, 7, 8]
TUPLE MANIPULATION
1.TUPLE T1=11,21,31,42,51 WHERE ITS 2 ND LAST ELEMENT
IS MISTYPED AS 42 CORRECT IT TO 41
#wap to take a tuple and update its elements
tuple1=11,21,31,42,51
tuple2=list(tuple1)
tuple2[-2]=41
tuple1=tuple(tuple2)
print(tuple1)
OUTPUT:
(11, 21, 31, 41, 51)
2.WAP TO INPUT A TUPLE AND SORT ITS ELEMENTS
#take a tuple and sort its elements
tuple1=list(eval(input('enter the elements of the
tuple-')))
tuple1.sort()
print(tuple(tuple1))
OUTPUT:
enter the elements of the tuple-(1,6,3)
(1, 3, 6)
3.WAP TO TAKE A TUPLE AND CHECK IF ALL ELEMENTS ARE
THE SAME
#take a tuple and check if all elements are the
same
tuple1=eval(input('enter the elements-'))
if min(tuple1)==max(tuple1):
print('all elements are the same ')
OUTPUT:
enter the elements-(1,1,1,1)
all elements are the same
4.WAP TO CHECK IF THE FIRST HALF OF TUPLE IS IN
ASCENDING ORDER
#wap to check if the 1st half of a tuple is in
ascending order
tuple1=eval(input('enter the values-'))
l=len(tuple1)/2
if len(tuple1)%2==0:
mid=int(l)
else:
mid=int(l+1)
new=tuple1[:mid]
tuple2=list(new)
tuple2.sort()
print(new)
print(tuple2)
if new==tuple(tuple2):
print('yes the 1st half of the tuple is sorted
in ascending order')
OUTPUT:
enter the values-(1,2,3,4,7,9,8,4,6)
(1, 2, 3, 4, 7)
[1, 2, 3, 4, 7]
5.WAP TO TAKE A TUPLE AND PRINT THE AVERAGE OF THE
NO’S WITHIN IT
#wap to calculate avg of
tuple1=eval(input('enter the numbers-'))
sm=0
for i in tuple1:
sm+=i
print('sum of values-',sm)
print('avg of values-',sm/len(tuple1))
OUTPUT:
enter the numbers-(1,2,3,4,5,6,7,8)
sum of values- 36
avg of values- 4.5
6.WAP TO CHECK IF THE LAST HALF OF TH ELEMENTS OF A
TUPLE ARE IN ASCENDING ORDER
#wap to check if the 1st half of a tuple is in
descending order
tuple1=eval(input('enter the values-'))
l=len(tuple1)/2
if len(tuple1)%2==0:
mid=int(l)
else:
mid=int(l+1)
new=tuple1[:mid]
tuple2=list(new)
tuple2.sort(reverse=True)
print(new)
print(tuple2)
if new==tuple(tuple2):
print('yes the 1st half of the tuple is sorted
in descending order')
OUPUT:
enter the values-(4,3,2,1,9,8,7,6)
(4, 3, 2, 1)
[4, 3, 2, 1]
yes the 1st half of the tuple is sorted in
descending order
7.WAP TO TAKE A TUPLE OF MARKS AND GIVE GRADE
AVG GRADE
>=75 A
60-74 B
50-59 C
<=50 D
tuple1=eval(input('enter the elements-'))
sm=sum(tuple1)
avg=sm/len(tuple1)
if avg>=75:
grade='A'
elif avg>=60 and avg<=74:
grade='B'
elif avg>=50 and avg<=59:
grade='C'
else:
grade='D'
print('marks=',sm)
print('average=',avg)
print('grade =',grade)
OUTPUT:
enter the elements-(75,80,89,90)
marks= 334
average= 83.5
grade = A
8.WAP TO CREATE TUPLES
A) TUPLES CONTAINING SQUARE OF INT 1 TO 50
#WAP TO TAKE A TUPLE WHIC CONTAINS SQUARES OF NOS FROM 1 TO 50
tuple1=[]
for i in range(1,51):
tuple1.append(int(i**2))
print(tuple(tuple1))
OUTPUT:
(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144,
169, 196, 225, 256, 289, 324, 361, 400, 441, 484,
529, 576, 625, 676, 729, 784, 841, 900, 961, 1024,
1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600,
1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304,
2401, 2500)
B) TUPLE –(A,BB,CCC,DDDD,EEEEE,…………………)
#wap to create a tuple (a,bb,ccc,.....)
alphabets=['a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t','u','v','w'
,'x','y','z']
tup=[]
for i in range(1,27):
tup.append(alphabets[i-1]*i)
tup=tuple(tup)
print(tup)
OR
#wap to create a tuple (a,bb,ccc,.....)
tup=[]
for i in range(97,123):
tup.append(chr(i)*(i-96))
print(tuple(tup))
OUTPUT:
('a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff',
'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj',
'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm',
'nnnnnnnnnnnnnn', 'ooooooooooooooo',
'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq',
'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss',
'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu',
'vvvvvvvvvvvvvvvvvvvvvv',
'wwwwwwwwwwwwwwwwwwwwwww',
'xxxxxxxxxxxxxxxxxxxxxxxx',
'yyyyyyyyyyyyyyyyyyyyyyyyy',
'zzzzzzzzzzzzzzzzzzzzzzzzzz')
9.WAP TO TAKE A TUPLE AND PRINT MIN AND MAX
#tuple min and max
tuple1=eval(input('enter the elements-'))
print(min(tuple1))
print(max(tuple1))
OUTPUT:
enter the elements-(1,2,3,4,5,9)
1
9
10.WAP TO MAKE A NESTED TUPLE OF STUDNTS CLASS ,
ROL NO AND NAME
#take a tuple to take roll no, name and class
tup=[]
while True:
roll_no=int(input('enter the roll number-'))
name=input('enter the name')
cl=input('enter the class')
tup1=(roll_no,name,cl)
tup.append(tup1)
q=input('if u want to add more press enter-')
if q!='':
break
print(tuple(tup))
OUTPUT:
enter the roll number-23
enter the nameSAI
enter the class11
if u want to add more press enter-
enter the roll number-21
enter the nameS
enter the class11
if u want to add more press enter-NO
((23, 'SAI', '11'), (21, 'S', '11'))