The list is a collection of items using proper sequence, It is similar to an array of other programming languages but the array contains a collection of similar types of elements but the list contains a collection of similar and dissimilar types of elements both.
The list provides an ordered collection of data, and it can store any type of element.
LIST provides a mutable object that means we can add, edit, and delete elements dynamically in the list.
Syntax of List:-
var = [item1,item2,item3,...]
x = [12,23]
Add element:-
x.append(24) #[12,23,24]
Delete an element from LIST:-
del x[0]
del x[0]
Modify List element:-
x[index]=value
x[index]=value
LIST Program to implement all operation:-
x = ["vegetable","rice","fruit","oil","flour"]
#print(x[0],x[1],x[2],x[3],x[4])
for i in range(0,len(x)):
print(x[i])
for value in x:
print(value)
#to display a particular range
print(x[3:])
print(x[2:4])
print(x[-4:-1])
print(x[-4])
print(x[:3])
print(x[:])
x.append('noodles')
print(x)
del x[0]
print(x)
x[4] = 'maggi'
print(x)
Another Example of List to append, edit and delete elements using Python Program:-
x = [12,23,34,11,67,89]
x.append(101) #append elements
del(x[0]) #delete element
x[0]=120 #modify elements
for i in range(0,len(x)):
print(x[i])
List Program Assignments:-
ASSIGNMENT
1) WAP to Display Prime elements in List?
2) WAP to display the following elements in List By Loop
[12,21,[1,2,3,[4,5],6,7]
[12,11,[2,5],[1,[1,2]]
3) WAP to find max elements in list?
4) WAP to arrange list elements
[1,2,3,1,1,5,2,3,4,5]
o/p [1,1,1,2,2,3,3,4,5,5]
5) WAP to calculate factorial of even number and table of odd number in the list?
all arithmetic operation
Addition and multiplication using LIST:-a= [1,2,3]
b = [4,5,6]
Addition:-
Addition:-
c=a+b + is used to combine two list elements in one list
[1,2,3,4,5,6]
[1,2,3,4,5,6]
Multiplication:-
a*2 * it will repeat the list of elements
Program of LIST to implement addition, multiplication "IN" and "IS" operator:-
a=[1,2,3]
b=a*2
print(b)
x=[1,2,3]
y=[4,5,6]
z=x+y
print(z)
del x[0];
print(x)
if(20 in a):
print("Found")
else:
print("NOT found")
y=x
print(x)
print(y)
if(x is y): #if same address then return true otherwise
print("equall")
else:
print("not equall")
....................................................................................................
Multidimensional LIST or Nested List:-
Program of LIST to implement addition, multiplication "IN" and "IS" operator:-
a=[1,2,3]
b=a*2
print(b)
x=[1,2,3]
y=[4,5,6]
z=x+y
print(z)
del x[0];
print(x)
if(20 in a):
print("Found")
else:
print("NOT found")
y=x
print(x)
print(y)
if(x is y): #if same address then return true otherwise
print("equall")
else:
print("not equall")
....................................................................................................
Multidimensional LIST or Nested List:-
If we write more than one list object using a nested sequence then it is called a nested List or multidimensional list.
If we want to display elements under matrix format then we will use the Multi dimension List.
It is the collection of rows and columns.
x = [[1,2,3],[4,5,6]]
for i in range(0,2):
for j in range(0,3):
print(x[i][j],end=' ')
print()
for j in range(0,3):
print(x[i][j],end=' ')
print()
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
We can display Multidimension List using for...in the loop if a number of elements of each row is not equall.
x = [[1,2,3,4,5],[3,4,6,7],[3,3,3]]
for i in x:
for j in i:
print(j,end='')
print()
WAP to multiply matrix using multi-dimensional list?
x = [[1,2],[4,5]]
y = [[2,4],[1,2]]
z= [[int]*2,[int]*2]
for i in range(0,2):
for j in range(0,2):
s =0
for k in range(0,2):
s = s + x[i][k]*y[k][j]
z[i][j]=s
print()
#display the output
for i in range(0,2):
for j in range(0,2):
print(z[i][j], end = " ")
print()
How to take input from Users in LIST?
In most the cases, LIST type is used to contain composite data from the database or any external file.
but if we want to take input from user's then we can take it.
x = []
size = int(input("number of elements")
for i in range(0,size):
x.append(input("enter item"))
for i in range(0,len(x)):
print(x[i])
......................................................................................................................................................................
WAP to convert multidimensional list into single dimension:-
y = [1,[2,3,[4,5]],[6,[7,8]]]
y1=y[0:1]
y2=y[1:2]
y21 = y2[0]
y22 = y21[0:2]
y23= y21[2:3]
y24 = y23[0]
y3 = y[2:3]
y31 = y3[0]
y32 = y31[0:1]
y33 = y31[1:]
y34 = y33[0]
print(y34)
print(y1+y22+y24+y32+y34)
Assignments:-
1) WAP to calculate the sum of List elements?
2) WAP to display prime elements in LIST?
3) WAP to divide one List into three different SubList?
4) WAP to reverse LIST elements?
5) WAP to combine three lists in one LIST?
6) WAP to create mark sheet using LIST with the same condition of IF-ELSE?
7) WAP to display only unique elements in LIST?
8) WAP to display repeated elements in LIST but it should display once?
9) WAP to sort the LIST element in descending order?
10) WAP to find max, second max, and third max elements in LIST without sorting?
Predefined in LIST:-
1) len():- return size of list
2) append():- it is used to add elements in a list dynamically.
3) extend():- it is used to add list elements into main list
4) remove():- it is used to delete the elements from list
5) min():- it is used to print the minimum element in the list
6) max():- it is used to print the maximum element in the list
8) sort():- it is used to sort the record in ascending order
9) reverse():- it is used to reverse the list of elements
10) index():- it is used to return the index of a particular element of the list
11) count():- it is used to return the number of elements of list.
12) list():- it is used to convert any other object to list type
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Example of List:-
x = [12,23,34,78,89,23,23,78]
print(len(x))
x.append(22)
print(x)
x.remove(23)
print(x)
x.index(12)
print(x.count(23))
y = [1,2,3]
#x=x+y
x.extend(y)
print(x)
x.sort()
print(x)
x.reverse()
print(x)
print(max(x))
print(min(x))
z = (1,2,3)
print(list(z))
Implement Slicing of LIST
l = [1,2,[3,4,5,[6,7]]]
A = l[0:2]
B = l[2:]
C=[]
D=[]
for i in B:
for j in i:
if type(j).__name__=="int":
C.append(j)
else:
for k in j:
D.append(k)
lst = A+C+D
print(lst)
Lokesh Rathore
ردحذف#Appling List Function :-
x = ["mobile","laptop","computer","charger","earphone"]
#For showing whole list :-
for i in range(0,len(x)):
print(x[i])
# WAP to calculate the sum of List elements?
ردحذفx=[1,2,3,4,5]
sum=x[0]+x[1]+x[2]+x[3]+x[4]
print(sum)
another way calculate the sum of List elements?
ردحذفsum=0
x=[20,30,40,50,60]
for value in range(0,len(x)):
sum=sum+x[value]
print("sum of elements is",sum)
#wap to check prime number in list
ردحذفx=[3,4,5,6,7,8]
for num in (x):
if num>1:
for i in range(2,num):
if num%2==0:
break
else:
print(num)
WAP to divide three list into one sublist?
ردحذفx = ["C", "C++", "DS", "JAVA", ".NET","Php","iOS"]
size1 = len(x)//3 #2
size2 = len(x)//3 #2
size3 = len(x)-(size1+size2) #3
print(x[0:size1])
print(x[size1:size1+size2])
print(x[size1+size2:size1+size2+size3])
# WAP to combine three lists in one LIST?
ردحذفa=[1,2,3]
b=[4,5,6]
c=[7,8,9]
d=a+b+c
print(d)
que. WAP to combine three lists in one LIST?
ردحذفans:
x=["1","2","3"]
y=["A","B","C"]
z=["a","b","c"]
print("x:",x)
print("y:",y)
print("z:",z)
list=x+y+z
print("list is:",list)
Q:- Calculate the Sum of List Elements?
ردحذفSolution:-
sum=0
x=[123,456,879,987,654,321]
for value in range(0,len(x)):
sum=sum+x[value]
print("sum of elements is",sum)
Q:- Divide one list into three sub-list ?
ردحذفSolution:-
x = ["Laptop", "Computer", "Android", "iPhone ios", "Windows","BlackBerry","Kali Linux"]
size1 = len(x)//3
size2 = len(x)//3
size3 = len(x)-(size1+size2)
print(x[0:size1])
print(x[size1:size1+size2])
print(x[size1+size2:size1+size2+size3])
WAP to reverse element of LIST?
ردحذفx = [12,23,34,11,67,89]
y = []
for i in range(len(x)-1,-1,-1):
y.append(x[i])
print(x[i])
print(x)
print(y)
WAP to find max,second max and third max elements without using sorting?
ردحذفx = [12,24,56,78,89,11,79,58]
m=0
s=0
t=0
for i in range(0,len(x)):
if m<x[i]:
t=s
s=m
m=x[i]
elif s<x[i]:
t=s
s=x[i]
elif t<x[i]:
t=x[i]
print("max is ",m, "second max is ",s," Third max is ",t)
Program to calculate sum of list elements--->>
ردحذفtotal=0
list=[12,24,36,48,60]
for ele in range(0,len(list)):
total= total + list[ele]
print("sum of list elements is-",total)
output-180
Program to display prime elements in list-->>
ردحذفstart=int(input("enter the value of start"))
end=int(input("enter the value of end"))
for num in range(start,end):
if num>1:
for p in range(2,num):
if(num%p==0):
break
else:
print(num)
output-enter the value of start5
enter the value of end32
5
7
11
13
17
19
23
29
31
Program to divide one list into three different sublist-->>
ردحذفu=["TCS","INFOSYS","MICROSOFT","GOOGLE","APPLE","YAHOO"]
sub1=len(u)//3#2
sub2=len(u)//3#2
sub3=len(u)-(sub1+sub2)#2
print(u[0:sub1])
print(u[sub2:sub1+sub2])
print(u[sub1+sub2:sub1+sub2+sub3])
WAP to sort the element of List:-
ردحذفx = [12,23,34,11,67,78,81]
for i in range(0,len(x)):
for j in range(i+1,len(x)):
if x[i]>x[j]:
x[i],x[j]=x[j],x[i]
print(x)
Program to reverse list of elements-->>
ردحذفx = [121,144,169,196]
y =[]
for i in range(len(x)-1,-1,-1):
y.append(x[i])
print(y)
Program to reverse list of elements--->>>
ردحذفp=["home","family","job","internet","wifi"]
q=[]
for i in range(4,-1,-1):
q.append(p[i])
print(q)
Program to combine 3 list in 1 list-->>
ردحذفp=["T20","Test","One day"]
q=[20,"Unlimited",50]
r=["Dhoni","Sehwag","Nehra"]
cricket=p+q+r
print("combined list is:",cricket)
#marks between 0 to 100
ردحذفcount=0
grace=0
distinction=""
sub=""
subject=[]
for i in range(5):
s=input("enter subject")
subject.append(s)
print("s1=",subject[0],"s2=",subject[1],"s3=",subject[2],"s4=",subject[3],"s5=",subject[4])
marks=[]
for i in range(5):
m=int(input("enter marks"))
marks.append(m)
if m>=0 and m<=100:
print("m1=",marks[0],"m2=",marks[1],"m3=",marks[2],"m4=",marks[3],"m5=",marks[4])
else:
print("enter marks between 0 to 100")
#if only one subject mark is <33 then the student will be suppl.
if m1<33:
grace=m1
count=count+1
sub=sub+s1
if m2<33:
grace=m2
count=count+1
sub=sub+s2
if m3<33:
grace=m3
count=count+1
sub=sub+s3
if m4<33:
grace=m4
count=count+1
sub=sub+s4
if m5<33:
grace=m5
count=count+1
sub=sub+s5
#check desinction
if m1>=75:
distinction=distinction+s1+" "
if m2>=75:
distinction=distinction+s2+" "
if m3>=75:
distinction=distinction+s3+" "
if m4>=75:
distinction=distinction+s4+" "
if m5>=75:
distinction=distinction+s5+" "
#ifall subject marks are >=33 then percentage and division should be calculated.
if count==0 or (count==1 and grace>=28):
per=(m1+m2+m3+m4+m5)/100
if per>33 and per<45:
print("pass with third division")
elif per<60:
print("pass with second division")
else:
print("pass with first division")
if(dist!=""):
print("distinction subject name is "+dist)
if c==1:
print("pass by grace and grace mark is "+str(33-g)+" subject is "+sub)
elif c==1:
print("suppl")
else:
print("fail")
#WAP to display only unique element in LIST?
ردحذفlist=[1,1,3,4,5,6,4,3]
result=[]
for i in list:
if i not in result:
result.append(i)
print(result)
#WAP to display repeated elements in LIST but it should display once
ردحذفlist=[1,1,3,4,5,6,4,3]#1,3
repeated=[]
for i in range(0,len(list)):
for j in range(i+1,len(list)):
if list[i]==list[j] and list[i] not in repeated:
repeated.append(list[i])
print(repeated)
Program to display unique elements of list-->>
ردحذفlist1= [125,125,625,3125,625,78125]
list2 = [7,5,125]
for values in list1:
if values not in list2:
list2.append(values)
print("Unique elements in the list-")
for values in list2:
print(values)
output-Unique elements in the list-
7
5
125
625
3125
78125
x=[65,90,89,56,40,46]
ردحذفfor i in range(0,len(x)):
for j in range(i+1,len(x)):
if x[i]<x[j]:
temp=x[i]
x[i]=x[j]
x[j]=temp
print("element after sorting=",x)
y=[1,4,5,6,7,9,10]
ردحذفm=0
sm=0
tm=0
for i in range(0,len(y)):
if m<y[i]:
tm=sm
sm=m
m=y[i]
elif sm<y[i]:
tm=sm
sm=y[i]
elif tm<y[i]:
tm=x[i]
print("max=",m"second max=",sm"third max ="tm)
Program to display repeated elements in LIST but it should display once-->>
ردحذفx=[1,2,2,3,3,4,4,6,5]
for i in range(0,len(x)):
for j in range(i+1,len(x)):
if(x[i]==x[j]):
print(x[j])
print("These are repeated elements in given list")
Q:- Program to reverse list of elements ??
ردحذفx = [190,280,360,450,550]
y =[]
for i in range(len(x)-1,-1,-1):
y.append(x[i])
print(y)
Q:- Program to reverse list of elements ??
ردحذفp=["nokia","vivo","oppo","samsung","apple"]
q=[]
for i in range(4,-1,-1):
q.append(p[i])
print(q)
Q:- Program to display repeated elements in LIST but it should display once ??
ردحذفx=[1,2,2,3,3,4,4,6,5]
for i in range(0,len(x)):
for j in range(i+1,len(x)):
if(x[i]==x[j]):
print(x[j])
print("These are repeated elements in given list :- ")
Q:- WAP to display only unique element in List ??
ردحذفlist=[1,1,3,4,5,6,4,3]
result=[]
for i in list:
if i not in result:
result.append(i)
print(result)
Q:- WAP to find Maximum, Second Maximum and Third Maximum elements without using sorting ??
ردحذفx = [123,45,654,987,958,52,456]
m=0
s=0
t=0
for i in range(0,len(x)):
if m<x[i]:
t=s
s=m
m=x[i]
elif s<x[i]:
t=s
s=x[i]
elif t<x[i]:
t=x[i]
print("First Max. No. is ",m, "Second Max. No. is ",s," Third max. No. is ",t)
sub=["PHY","CHEM","MATHS","ENG","HINDI"]
ردحذفmarks=[84,30,56,67,89]
count=0
total=0
gm=0
dist=''
s=''
for i in range(0,len(sub)):
if marks[i]>=0 and marks[i]<=100:
if marks[i]<33:
count=count+1
s=s+sub[i]+ " "
gm=marks[i]
else:
if marks[i]>=75:
dist = dist+ sub[i] + " "
else:
print("All subject marks should be 0 to 100")
break
total=total+marks[i]
else:
if count==0 or (count==1 and gm>=29):
per= total/5
if per>33 and per<45:
print("Third division with {0} %".format(per))
elif per<60:
print("Second division with {0} %".format(per))
else:
print("First division with {0} %".format(per))
if gm>0:
print("You are pass by grace and grace marks is ",str(33-gm), "grace subject is ",s)
if dist!=0:
print("Distinction subject name is ",dist)
elif count==1:
print("suppl in ",s)
else:
print("fail in ",s)
Program to display repeated elements in list but it will be displayed only one time-->>
ردحذفx=[1,4,2,3,3,2,3,4,4,2,4]
for i in range(0,len(x)):
for j in range(i+1,len(x)):
if x[i]==x[j]:
x[j]=0
for i in range(0,len(x)):
if x[i]!=0:
print(x[i])
WAP to calculate the sum of List elements?
ردحذفx=[2,4,6,8]
s=0
for i in range (0,len(x)):
s=s+x[i]
print(s)
wap to find prime no. form list
ردحذفx=[3,4,5,6,7,8,11,17,18,9,27]
for num in (x):
if num > 1:
for i in range(2,num):
if num%i ==0:
break
else:
print(num)
wap to divide single list into three list:
ردحذفx=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,25]
for num in x:
if num%2 ==0:
print(num,end='')
for j in x:
if j%3 ==0 and j%2 !=0:
print( j,end=' ')
for k in x:
if k%2 !=0 and k%3 !=0:
print(k,end=' ')
#WAP to calculate the sum of List elements?
ردحذفy =[90,80,85,80,71]
sum = 0
for i in range(0,len(y)):
sum=sum + y[i]
print((sum))
# WAP to combine three lists in one LIST?
ردحذفx=[22,11,33]
y=[55,44,66]
z=[77,66,99]
a=x+y+z
print(a)
lst = []
ردحذفnum = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Sum of elements in given list is :", sum(lst))
use logic not predefine method
حذفstart = 100
ردحذفend = 2
print("Prime numbers between", end, "and", start, "are:")
for num in range(end, start + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
display prime elements in list
حذف# Python code to split a list
ردحذف# into sublists of given length.
from itertools import islice
# Input list initialization
Input = [1, 2, 3, 4, 5, 6, 7]
# list of length in which we have to split
length_to_split = [2, 1, 3, 1]
# Using islice
Inputt = iter(Input)
Output = [list(islice(Inputt, elem))
for elem in length_to_split]
# Printing Output
print("Initial list is:", Input)
print("Split length list: ", length_to_split)
print("List after splitting", Output)
Wrong, split list elements into three different sub list
حذفstudentNames = ["abcd", "efgh", "ijkl", "mnop"]
ردحذفstudentNames.reverse()
print(studentNames)
Without using reverse()
حذفlist1 = [1,2,3,4]
ردحذفlist2 = [5,6,7,8]
list3 = [9,10,11,12]
list1 = list1 + list2 + list3
print("the modified list is " + str(list1))
Without using +
حذفABHISHEK GOYAL
ردحذفWAP to calculate the sum of List elements?
x=[]
sum=0
size=int(input("enter size of list"))
for i in range(0,size):
x.append(int(input("input value")))
for i in range (0,len(x)):
print(x[i],end=' ')
for i in range(0,len(x)):
sum=sum+x[i]
print("sum is ", sum)
ABHISHEK GOYAL
ردحذف# WAP to calculate the sum of List elements?
x=[3,4,12,55,90]
y=len(x)
sum=0
for i in range (0,y):
sum=sum+int(x[i])
print(sum)
ABHISHEK GOYAL
ردحذفWAP to divide three list into one sublist?
x = [2,33,56,78,88,90]
size1 = len(x)//3
size2 = len(x)//3
size3 = len(x)-(size1+size2)
print(x[0:size1])
print(x[size1:size1+size2])
print(x[size1+size2:size1+size2+size3])
#To reverse a list
ردحذفx = [34,7625,9876,19]
print(x[::-1])
#WAP to divide one List into three different SubList?
ردحذفx = [34,7625,9876,19,50,80,80,19,71]
l=0
m=len(x)
for i in range(l,m,3):
l=i
print(x[l:l+3])
#WAP to create marksheet using LIST with the same condition of IF-ELSE?
ردحذفm= []
for i in range(0,5):
n = int(input("enter marks"))
m.append(n)
print(m)
t =sum(m)
print("Total =",t)
if(m[0]>=28 and m[0]<33) or (m[1]>=28 and m[1]<33) or (m[2]>=28 and m[2]<33) or (m[3]>=28 and m[3]<33) or (m(4)>=28 and m(4)<33):
print("Pass with grace")
if t>=300:
print("percentage is = " ,(t/5))
print("First Division")
elif t<300 and t>=250:
print("Percentage is = ", (t/5))
print("Second divison")
elif t<250 and t>= 165:
print("Percent is = ",(t/5))
print("Third division")
else:
print("Fail")
ABHISHEK GOYAL
ردحذف#WAP to display only unique element in LIST?
list=[1,1,3,4,5,6,4,3]
result=[]
for i in list:
if i not in result:
result.append(i)
ABHISHEK GOYAL
ردحذفWAP to display repeated elements in LIST but it should display once?
list=[1,1,3,4,5,6,4,3]
result=[]
repeat=[]
for i in list:
if i not in result:
result.append(i)
else:
repeat.append(i)
print("repeted elements"," ",repeat)
# PYTHON (6 to 7 PM BATCH)
ردحذف# Program to display Prime Elements in LIST
a= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
for x in a:
for i in range(2,x):
if (x % i) == 0:
print("Not a Prime No.:-\t",x)
break
else:
print("Prime :-\t",x)
# PYTHON (6 to 7 PM BATCH)
ردحذف# Program to calculate the sum of List Elements.
a= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
print("Sum of List Elements :-\t",sum(a))
# OUTPUT :- Sum of List Elements :- 153
# PYTHON (6 To 7 PM BATCH)
ردحذف# Program to Divide One List into Four Different SubList
a= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
x=0
y=len(a)
l=[]
for j in range(x,y,4):
x=j
l.append(a[x:x + 4])
print("l =",l)
#OUTPUT :- l = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
# PYTHON ( 6 To 7 PM BATCH)
ردحذف# Program to combine 4 lists in 1 LIST
a = [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]
x=0
y=len(a)
l=[]
for j in range(x,y):
for i in range(0,4):
l.append(a[j][i])
print(l)
# OUTPUT :- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
# PYTHON (6 TO 7 PM BATCH)
ردحذف#CODE for Marksheet using LIST with the same condition of IF-ELSE
lst = []
n = int(input("Please Enter the No. of Students:- "))
for i in range(0, n):
ele = [input("Name of Student"), int(input("Total Marks(5-500")),"Fail"]
if ele[1]>=425:
ele[2]="High Dictinction"
elif ele[1]<425 and ele[1]>350:
ele[2]="Dictinction"
elif ele[1] <=350 and ele[1] >=165:
ele[2] = "Pass"
else:
pass
lst.append(ele)
print("\t MarkSheet of All Students")
print()
for i in range(0,3):
print("Name :- \t",lst[i][0])
print("Total Marks :- \t",lst[i][1])
print("Final Grade :- \t",lst[i][2])
print()
# PYTHON ( 6 to 7 PM BATCH )
ردحذف# CODE to calculate the sum of List elements(with Logic)
lst = [11,22,33,44,55,66,77,88,99]
x= len(lst)
y=0
for i in range(len(lst)):
y = y+lst[i]
print("Sum of List Elements :-\t",y)
#OUTPUT :- Sum of List Elements :- 495
# PYTHON (6 To 7 PM BATCH)
ردحذف# CODE to display only unique element in LIST
lst = [5,7,9,11,33,55,66,77,88,5,22,77,7]
l=[]
for x in lst:
y = lst.count(x)
if y==1:
l.append(x)
else:
pass
print("Unique Element in LIST:- ",l)
# OUTPUT - Unique Element in LIST:- [9, 11, 33, 55, 66, 88, 22]
# PYTHON (6 To 7 PM BATCH)
ردحذف# CODE to display repeated elements in LIST
lst = [5,7,9,11,33,55,66,77,88,5,22,77,7]
l=[]
for x in lst:
y = lst.count(x)
if y>1:
l.append(x)
lst.remove(x)
else:
pass
print("Repeated Elements in LIST:- ",l)
# OUTPUT - Repeated Elements in LIST:- [5, 77, 7]
# PYTHON ( 6 To 7 PM BATCH)
ردحذف# CODE to Sort the LIST Element in Descending Order
my_list = [-15, -26, 15, 1, 23, -64, 23, 76]
new_list = []
while my_list:
y = my_list[0]
for x in my_list:
if y<x:
y=x
new_list.append(y)
my_list.remove(y)
print(new_list)
#OUTPUT :- [76, 23, 23, 15, 1, -15, -26, -64]
# PYTHON (6 To 7 PM BATCH)
ردحذف# Program to Find Max, Second Max, and Third Max Elements in LIST
# Only Whole Number
my_list = [ 6, 5, 4, 3, 2,11,321,456,5,4,86,435, 7]
a =2
b =1
c =0
for i in my_list:
if i >= a:
c = b
b = a
a = i
if i < a and i >= b:
c = b
b = i
if i < b and i >= c:
c = i
else:
pass
print("MAX :-",a)
print("Second MAX :-",b)
print("Third MAX :-",c)
# PYTHON ( 6 To 7 PM BATCH)
ردحذف# Program to Find Max, Second Max, And Third Max Elements in LIST
my = [ 6, 5, 4, 3, 2,11,321,456,5,4,86,435, 7]
my_list = list(set(my))
list1 = []
while my_list:
a = my_list[0]
for i in my_list:
if i>a:
a=i
else:
pass
list1.append(a)
my_list.remove(a)
print("MAX :- \t",list1[0])
print("Second MAX:- \t",list1[1])
print("Third MAX :- \t",list1[2])
#SUM OF LIST:
ردحذفlist=[2,27,22,72,77,7]
sum=0
for i in list:
sum=sum+i
print(sum)
#Reverse_Every_List_Element
ردحذفrev=[27,92,74,49,2,775,93647]
for i in rev:
res=0
while(i>0):
r=i%10
res=(res*10)+r
i=i//10
print(res,end=" ")
#Find_In_List
ردحذفfun=[72,4,55,578]
f=578
for i in fun:
if i==f:
index=fun.index(i)
print(i, " is at the index ", index)
#ASCENDING&DESCENDINGLIST
ردحذفlist=[12,7,3,88,2]
#ASCENDING
for i in range(0,len(list)):
for j in range(i+1,len(list)):
if (list[i]>list[j]):
list[i],list[j]=list[j],list[i]
print('Ascending ordered list: ',list)
#DESCENDING
for i in range(0,len(list)):
for j in range(i+1,len(list)):
if (list[i]<list[j]):
list[i],list[j]=list[j],list[i]
print('Descending ordered list: ',list)
#UNIQUE_ELEMENT
ردحذفlist=[1,1,2,7,9,9]
unique=[]
for i in list:
y=list.count(i)
if y==1:
unique.append(i)
print(unique)
#DuplicateElementsInList
ردحذفl=[2,2,4,6,7,4,8]
for i in range(0,len(l)):
d=0
for j in range(i+1,len(l)):
if(l[i]==l[j]):
d+=1
if d>0:
print(l[i])
#PrimeNoAndSum
ردحذفlist=[1,2,3,4,5,6,7,9]
sum=0
for i in range(0,len(list)):
flag=True
for j in range(2,list[i]):
if (list[i]%j==0):
flag=False
if flag==True:
print(list[i])
sum+=list[i]
print(sum)
#Reverce List Element "By Surendra"
ردحذفx = [1,2,3,4,5,"Ram","Shyam",20.12,50.78]
for i in reversed(x):
print(i)
#Divide one list into three sub-list ?
ردحذف#Ravi Vyas
lista=[1,2,3,4,5,6,7,8,9]
l=len(lista)
s=int(l/3)
print(lista[:s])
print(lista[s:s+3])
print(lista[s+3:])
#WAP to combine three lists in one LIST?
ردحذف#Ravi Vyas
lista=[1,2,3]
listb=[4,5,6]
listc=[7,8,9]
listd=lista+listb+listc
print(listd)
#WAP to display prime elements in LIST?
ردحذفa=[1,2,3,4,5,6,7,8]
for i in a:
if i > 1:
for j in range(2, i):
if (i % 2) == 0:
break
else:
print(i,end=' ')
#Reverce List Iteam "By Surendra"
ردحذفL = [34,7625,9876,19]
print(L[::-1])
#Reverce List Iteam "By Surendra"
ردحذفL = [34,7625,9876,19]
print(L[::-1])
#Reverce List Iteam "By Surendra"
ردحذفL = [34,7625,9876,19]
print(L[::-1])
# WAP to reverse LIST elements? #
ردحذف# Vishal Baghel #
x1 = [2,3,4,5,6,7,8,9,10,11]
x2 = []
for i in range(len(x1)-1,-1,-1):
x2.append(x1[i])
print(x1)
print(x2)
#WAP to display repeated elements in LIST but it should display once?
ردحذفa=[1,2,3,4,2,4]
for i in range(0,len(a)):
for j in range (i+1,len(a)):
if(a[i]==a[j]):
temp=a[i]
a[i]=a[j]
a[j]=temp
print(a[i],end=' ')
SUNIL PARMAR
ردحذف#Unique number progeam
x=[12,77,45,77,44,55,44,58,48]
for i in range (0,len(x)):
count=0
z=x[i]
for j in range (0,len(x)):
if (z==x[j]):
count+=1
else:
if (count==1):
print(z)
# Python (6 to 7 PM )
ردحذف# Program to Find The Maximum from repeated Element.
lst = [1,1,1,2,2,2,2,5,5,5,6,6,6,6,6,6,6,3,3,3,3,3,34,4,4]
se = set(lst)
a = 0
c=0
for j in se:
a = lst.count(j)
if a>1:
if j>c:
c = j
print(c)
#wap to calculate the sum of list element ?
ردحذفx=[1,2,3]
sum=x[0]+x[1]+x[2]
print(sum)
#another way
x=[2,5,6,6,8,9,11]
for i in range(0,len(x)):
sum=sum+x[i]
print(sum)
# wap to display prime element in list ?
x=[1,3,5,4,7,8]
for num in x:
if num<=1:
continue
for i in range(2,num):
if (num%i)==0:
break
else:
print(num)
# wap divide three list into one sublist?
x=['apple','orange','mango','banana','graps','pineapple']
print(x[:3])
print(x[2:5])
print(x[3:6])
# wap to reverse element of list?
x=[1,2,3,4,5,6]
y=[]
for i in range(len(x)-1,-1,-1):
y.append(x[i])
print(y)
#wap to combine three list ?
x=[1,2,3]
y=[4,5,6]
z=[7,8,9]
c=x+y+z
print(c)
# wap to display unique element in list?
L=[2,5,8,2,1,3,1,5,2,4]
R=[]
for i in L:
if i not in R:
R.append(i)
print(i)
# WAP to sort the LIST element in descending order?
x=[5,3,4,2,11,48,15]
for i in range(0,len(x)):
for j in range(i+1,len(x)):
if x[i]>x[j]:
x[i],x[j]=x[j],x[i]
print(x)
# WAP to display repeated elements in LIST but it should display once?
x=[2,5,3,6,2,4,3,2,5,9,1,2]
y=[]
for i in range(0,len(x)):
for j in range(i+1,len(x)):
if x[i]==x[j] and x[i] not in y:
y.append(x[j])
print(y)
# WAP to find max, second max, and third max elements in LIST without sorting?
x=[8,8,2,4,7,2,1,8,8,1]
m=0
s_max=0
t_max=0
for i in range(0,len(x)):
if m<x[i]:
s_max=m
t_max=s_max
m=x[i]
elif s_max<x[i] and x[i]!=m:
t_max=s_max
s_max=x[i]
print(m,"",s_max,"",t_max)
#10) WAP to find max, second max, and third max elements in LIST without sorting?
ردحذفvar=[10,20,21,5,51,32,66,23,43,61]
l=[ ]
sl=[ ]
tl=[ ]
for i in range(0,len(var)):
for j in range(i+1,len(var)):
if var[i]<var[j]:
var[i],var[j]=var[j],var[i]
l=(var[0])
sl=(var[1])
tl=(var[2])
print("largest elament in list",l)
print("second largest element in list",sl)
print("third largest element in list",tl)
#9) WAP to sort the LIST element in descending order?
ردحذفvar=[23,10,5,41,13,32,22,54,11]
print(var)
for i in range(0,len(var)):
for j in range(i+1,len(var)):
if var[i]<var[j]:
var[i],var[j]=var[j],var[i]
print(var)
#7) WAP to display only unique element in LIST?
ردحذفvar=[10,10,21,32,43,34,45,32,34]
x=[]
for i in var:
if i not in x:
x.append(i)
print(x)
#5) WAP to combine three lists in one LIST?
ردحذفa=[10,20,30]
b=[40,50,60]
c=[70,80,90]
d=a+b+c
print(d)
#4) WAP to reverse LIST elements?
ردحذفvar=[10,20,30,40,50,60,70,80,90]
var1=[ ]
for i in range(len(var)-1,-1,-1):
var1.append(var[i])
print(var)
print(var1)
#3) WAP to divide one List into three different SubList?
ردحذفList=[10,5,2,14,21,32,23,45,33,45,32,87]
a=List[:4]
b=List[4:8]
c=List[8:]
print(a)
print(b)
print(c)
#1) WAP to calculate the sum of List elements?
ردحذفx=[10,12,14,16,20,21]
Sum=0
for i in range (0,len(x)):
Sum=Sum+x[i]
print(Sum)
#8) WAP to display repeated elements in LIST but it should display once?
ردحذفa=[10,10,32,43,43,21,12,13,13,21]
b=[]
for i in range(0,len(a)):
for j in range(i+1,len(a)):
if a[i]==a[j] and a[i] not in b:
b.append(a[i])
print(b)
#NIKHIL SINGH CHOUHAN
ردحذفx=[3,4,5,6,7,8,9,23,21,91,31,101]
for num in x:
if num>1:
for i in range (2,num):
if(num%i)==0:
break
else:
print(num)
#2) WAP to display prime elements in LIST?
ردحذفx=[3,4,5,6,7,8,9,23,21,91,31,101]
for num in x:
if num>1:
for i in range (2,num):
if(num%i)==0:
break
else:
print(num)
Mark sheet program solution using LIST?
ردحذفsub = ['phy','chem','maths','eng','hindi']
marks = [55,78,89,59,85]
c=0
total=0
g=0
s = ""
per=0
dist=" "
for i in range(0,len(marks)):
if marks[i]<0 or marks[i]>100:
print("Invalid marks")
break
else:
if marks[i]<33:
c=c+1
g=marks[i]
s = s + sub[i] + " "
if marks[i]>=75:
dist = dist + sub[i] + " "
total = total +marks[i]
else:
if c==0 or c==1 and g>=28:
if c==1:
per= (total+(33-g))/5
print("pass by grace in "+s)
else:
per= (total)/5
if per>=33 and per<45:
print("pass with third division "+str(per) + "%")
elif per<60:
print("pass with second division "+str(per) + "%")
else:
print("pass with first division "+str(per) + "%")
if dist!="":
print("distinction subject are "+dist)
elif c==1:
print("suppl in "+s)
else:
print("failed in "+s)
# Ankit Saxena
ردحذف# Using the Predefined in List Program
lst=[]
user=int(input("Enter Max Size: "))
for i in range(user):
value=input("Enter Value: ")
lst.append(value)
print("Original list is:", lst)
print("Length of list is:",len(lst))
print("Max value in the list is:", max(lst))
print("Min value in the list is:", min(lst))
x=input("Enter value to count:")
y=lst.count(x)
print(x,"Appears in the list", y," times")
x=input("Enter value whose index you want to get:")
index=lst.index(x)
print(x,"Found at",index,"index.")
value=input("Enter value to insert:")
i=int(input("Enter index where you want to insert:"))
lst.insert(i,value)
print(lst)
value=input("Enter value to remove from the list:")
lst.remove(value)
print("Original list",lst)
lst.reverse()
print("List after reverse list is", lst)
lst.sort()v
print("List after sorting list", lst)
Divide one list into three sub-list ?
ردحذفSolution:-
x = ["Laptop", "Computer", "Android", "iPhone ios", "Windows","BlackBerry","Kali Linux"]
size1 = len(x)//3
size2 = len(x)//3
size3 = len(x)-(size1+size2)
print(x[0:size1])
print(x[size1:size1+size2])
print(x[size1+size2:size1+size2+size3])
#Shivam Shukla
ردحذف#1) WAP to calculate the sum of List elements?
x=[1,2,3,4,5]
d=0
for i in x:
d+=i
print(d)
#Shivam Shukla
ردحذف#2) WAP to display prime elements in LIST?
v=[7,15,27,17,18]
v1=[]
for i in v:
for j in range(2,i):
if i%j==0:
break;
if i==j+1:
v1.append(i)
print(v1)
#Shivam Shukla
ردحذف#3) WAP to divide one List into three different SubList?
list1=[1,12,23,34,45,56,67,78,89]
count=0;
for i in list1:
count+=1;
k=count//3
s=0
list2=list1[s:k*1]
s=k*1
list3=list1[s:k*2]
s=k*2
list4=list1[s:]
print(list2,list3,list4);
#Shivam Shukla
ردحذف#4) WAP to reverse LIST elements?
lst=[1,2,3,4,5,6,7,8,9,10]
count=0;
for i in lst:
count+=1;
for i in range(0,(count//2)+1):
lst[i],lst[count-i-1]=lst[count-i-1],lst[i];
print(lst)
#Shivam Shukla
ردحذف#5) WAP to combine three lists in one LIST?
list1=[12,23]
list2=[34,45]
list3=[56,67]
print(list1+list2+list3)
#Shivam Shukla
ردحذف#6-6.1) WAP to create marksheet using LIST with the same condition of IF-ELSE? (theNormal)
subj=['Hindi','English','Math','Physics','Chemestry']
marks=[98,85,76,52,29]
x=mark=0; sub=dist='';
if((marks[0]>=0 and marks[0]<=100) and (marks[1]>=0 and marks[1]<=100) and (marks[2]>=0 and marks[2]<=100) and (marks[3]>=0 and marks[3]<=100) and (marks[4]>=0 and marks[4]<=100)): #1
if(marks[0]<33 or marks[0]>=75): #2
if(marks[0]<33): #3
x=x+1; mark=marks[0]; sub += subj[0] +" "; #4
else: #3
dist= dist+ subj[0] +" "; #4
if(marks[1]<33 or marks[1]>=75): #2
if(marks[1]<33): #3
x=x+1; mark=marks[1]; sub += subj[1] + " "; #4
else: #3
dist= dist+ subj[1] + " "; #4
if(marks[2]<33 or marks[2]>=75): #2
if(marks[2]<33): #3
x=x+1; mark=marks[2]; sub += subj[2] +" "; #4
else: #3
dist= dist+ subj[2] +" "; #4
if(marks[3]<33 or marks[3]>=75): #2
if(marks[3]<33): #3
x=x+1; mark=marks[3]; sub=sub+ subj[3] +" "; #4
else: #3
dist= dist+ subj[3] +" "; #4
if(marks[4]<33 or marks[4]>=75): #2
if(marks[4]<33): #3
x=x+1; mark=marks[4]; sub= sub + subj[4] + " "; #4
else: #3
dist= dist+ subj[4] +" "; #4
if((x==0) or (x==1 and mark>=28)): #2
if(x==0): #3
per = (m1+m2+m3+m4+m5)/5; #4
else: #3
per = (marks[0]+marks[1]+marks[2]+marks[3]+marks[4]+(33-mark))/5 #4
if(per>=33 and per<45): #3
print("Pass with Third division "+str(per) + "%") #4
elif(per>=45 and per<60): #3
print("Pass with Second division "+str(per) + "%") #4
else: #3
print("Pass with First Division "+str(per) + "%") #4
if(x==1): #3
print("You are pass by grace and grace mark is "+str(33-mark) + ", subject is "+sub) #4
if(not(dist=="")): #3
print("Distinction subject name are "+dist) #4
elif(x==1): #2
print("suppl in "+sub); #3
else: #2
print("failed in "+sub); #3
else: #1
print("invalid marks"); #2
#Shivam Shukla
ردحذف#6-6.2) WAP to create marksheet using LIST with the same condition of IF-ELSE? (with UserChoise nd within a loop)
subj=[]
marks=[]
for aivaee in range(0,5):
subj.append(input("Enter a subjectName : "));
marks.append(int(input("Enter a it's subjectMarks : ")))
x=mark=0; sub=dist='';
if((marks[0]>=0 and marks[0]<=100) and (marks[1]>=0 and marks[1]<=100) and (marks[2]>=0 and marks[2]<=100) and (marks[3]>=0 and marks[3]<=100) and (marks[4]>=0 and marks[4]<=100)): #1
for i in range(0,5):
if(marks[i]<33 or marks[i]>=75): #3
if(marks[i]<33): #4
x=x+1; mark=marks[i]; sub += subj[i] +" "; #5
else: #4
dist= dist+ subj[i] +" "; #5
if((x==0) or (x==1 and mark>=28)): #2
if(x==0): #3
per = (marks[0]+marks[1]+marks[2]+marks[3]+marks[4])/5; #4
else: #3
per = (marks[0]+marks[1]+marks[2]+marks[3]+marks[4]+(33-mark))/5 #4
if(per>=33 and per<45): #3
print("Pass with Third division "+str(per) + "%") #4
elif(per>=45 and per<60): #3
print("Pass with Second division "+str(per) + "%") #4
else: #3
print("Pass with First Division "+str(per) + "%") #4
if(x==1): #3
print("You are pass by grace and grace mark is "+str(33-mark) + ", subject is "+sub) #4
if(not(dist=="")): #3
print("Distinction subject name are "+dist) #4
elif(x==1): #2
print("suppl in "+sub); #3
else: #2
print("failed in "+sub); #3
else: #1
print("invalid marks"); #2
#Shivam Shukla
ردحذف#7) WAP to display only unique element in LIST?
list1=[12,23,45,56,12,56]
list2=[]
count=0
for i in list1:
count+=1
for i in list1:
k=0
for j in list1:
if(i==j):
k+=1;
if(k==1):
list2.append(i)
print(list2)
#Shivam Shukla
ردحذف#9) WAP to sort the LIST element in descending order?
list1=[1,8,6,4,7,2];
count=0;
for i in list1:
count+=1;
for i in range(0,count-1):
for j in range(i+1,count):
if(list1[i]>list1[j]):
list1[i],list1[j]=list1[j],list1[i]
print(list1);
#Reversing elements of list
ردحذفx=[1,2,3,4,5,6,7,8,9,10,11,12]
print("Original List:::",x)
for i in range(0,int(len(x)/2)):
x[i],x[len(x)-1-i]=x[len(x)-1-i],x[i]
print("List in Reverse:::",x)
#Shivam Shukla
ردحذف#8) WAP to display repeated elements in LIST but it should display once?
list1=[12,23,45,56,12,56,17];
list2=[];
count=0;
for i in list1:
count+=1;
for i in list1:
k=0;
for j in list2:
if(i==j):
k=1;
break;
if(k==0):
list2.append(i);
print(list2);
WAP to calculate the sum of List elements-
ردحذفa=[1,2,3,4,5,6]
s=0
for i in range(0,6):
s=s+a[i]
print(s)
WAP to display prime elements in LIST
ردحذفx=[22,24,25,26,31,37,27,28,29]
w=[]
for i in range(0,9):
a=x[i]
for j in range(2,a):
if a%j==0:
break
else:
w.append(a)
print(w)
WAP to divide one List into three different SubList
ردحذفx=[1,2,3,4,5,6,7,8,9,10,11,12,13]
l=len(x)
s=l//3
s1=x[0:s]
s2=x[s:2*s]
s3=x[2*s:l]
print(s1,s2,s3)
WAP to reverse LIST elements
ردحذفa=[1,2,3,4,5]
s=a[::-1]
print(s)
WAP to reverse LIST elements
ردحذفa=[12,13,14,15,16,17,18]
s=[]
l=len(a)
for i in range(l-1,-1,-1):
x=a[i]
s.append(x)
print(s)
OR
a=[1,2,3,4,5] #reverse
s=a[::-1]
print(s)
WAP to combine three lists in one LIST
ردحذفa=[1,2,3]
b=[4,5,6]
c=[7,8,9]
x=a+b+c
print(x)
WAP to create marksheet using LIST with the same condition of IF-ELSE
ردحذفs=['English','Hindi','Science','Social','Mathemaics']
m=[]
for i in range(0,len(s)):
r=float(input("enter marks for "+s[i]+ " out of 100"))
m.append(r)
p=(sum(m)/500)*100
for i in range(0,len(s)):
for j in range(i,i+1):
print(s[i],":",m[j])
if p>=33:
print("pass")
else:
print("fail")
WAP to display only unique elements in LIST
ردحذفa=[12,13,14,12,15,16,16,13,17] #unique
x=[]
for i in range(0,len(a)):
if a.count(a[i])==1:
x.append(a[i])
else:
continue
print(x)
WAP to display repeated elements in LIST but it should display once
ردحذفa=[12,13,14,12,15,16,16,13,17]
x=[]
for i in range(0,len(a)):
if a.count(a[i])!=1:
if a[i] not in x:
x.append(a[i])
else:
continue
else:
continue
print(x)
WAP to sort the LIST element in descending order
ردحذفx=[23,65,43,62,71]
x.sort(reverse=True)
print(x)
WAP to find max, second max, and third max elements in LIST without sorting
ردحذفx=[34,86,27,23,12,90]
m1=max(x)
x.remove(m1)
m2=max(x)
x.remove(m2)
m3=max(x)
x.remove(m3)
print(m1,m2,m3)
WAP to calculate the sum of List elements?
ردحذفx=[20,30,40,50,60]
sum=0
for value in range(0,len(x)):
sum=sum+x[value]
print("sum of elements is",sum)
x=[1,22,54,8,46,9,65,41]
ردحذفsum=0
for i in range(0,len(x)):
sum=sum+x[i]
print(sum)
x=[5,9,7,6,8,23,4,6,73,45,66,99]
ردحذفfor num in x:
if num>1:
for i in range(2,num):
if(num%i)==0:
break
else:
print(num)
x=[1,2,5,6,54,9,1,9,1,8,5,1,5,48,4,654,8]
ردحذفprint(len(x))
s1=len(x)//3
s2=len(x)//3
s3=len(x)-(s1+s2)
print(s1,s2,s3)
x=[1,5,9,7,8,6,4,2,3,5,9,8,6]
ردحذفx.reverse()
print(x)
x=[8,9,'a']
ردحذفy=[1,2,3]
z=[2.5,7.6,47]
s=x+y+z
print(s)
s=["English","Hindi","Maths","Bio","Sci"]
ردحذفn=[56,87,65,77,32]
total=0
g=0
for i in range(0,len(n)):
if n[i]>33:
print()
if n[i]<33 and n[i]>=28:
g=33-n[i]
print("your are on grace",g)
total=total+n[i]
p=(total*100)/500
print("your percentage is "+str(p))
if p<=100 and p>=75:
print("your are pass by A grade")
elif p<75 and p>=60:
print("your are pass by B grade")
elif p<60 and p>=45:
print("your are pass by c grade")
elif p<45 and p>=33:
print("you are pass by d grade")
else:
print("you are fail")
x=[1,1,5,6,8,9,4,5,4,5,6,8,9,4]
ردحذفr=[]
for i in x:
if i not in r:
r.append(i)
print(r)
x=[5,8,9,8,5,6,8,1,2,4,5,8,4,6,4,8,61,4]
ردحذفy=[]
for i in range(0,len(x)):
if x.count(x[i])!=1:
if x[i] not in y:
y.append(x[i])
else:
continue
else:
continue
print(y)
x=[58,56,98,16,15,18,952,46,77,652]
ردحذفm1=max(x)
x.remove(m1)
m2=max(x)
x.remove(m2)
m3=max(x)
x.remove(m3)
print(m1,m2,m3)
#1) WAP to calculate the sum of List elements?
ردحذفlist1=[]
s=0
length=int(input("enter the length of list:-"))
for i in range(0,length):
n=int(input("enter the element:-"))
list1.append(n)
s=s+list1[i]
print(s)
#2) WAP to display prime elements in LIST?
ردحذفlist1=[]
i=1
l=int(input("enter the length of list:="))
for i in range(0,l):
num=int(input("enter the element:-"))
list1.append(num)
for num in range(0,len(list1)):
if list1[num] >1:
for j in range(2, list1[num]):
if (list1[num] % j) == 0:
break
else:
print(list1[num])
#WAP to divide one List into three different SubList?
ردحذفa=[]
b=[]
c=[]
x=[10,20,3,4,5,6,7,8,9,10]
print("list is:-",x)
for i in range(0,len(x)):
if i<3:
a.append(x[i])
if i>=3 and i<6:
b.append(x[i])
if i>=6:
c.append(x[i])
print("three sub list is:-")
print(a)
print(b)
print(c)
#4) WAP to reverse LIST elements?
ردحذفx=[]
n=int(input("enter the length of list:-"))
for i in range(0,n):
num=int(input("enter the elements:-"))
x.append(num)
print("list is:-",x)
print("reverse list is:-",x[::-1])
#5) WAP to combine three lists in one LIST?
ردحذفa=[1,8,6,3]
b=[2,6,3,4]
c=[8,9,5,1]
x=[]
for i in range(0,len(a)):
x.append(a[i])
x.append(b[i])
x.append(c[i])
print(x)
#WAP to find max, second max, and third max elements in LIST without sorting?
ردحذفmax=0
smax=0
tmax=0
x=[1,5,6,9,1118,114,16,3]
print("list is:-",x)
for i in range(0,len(x)):
if x[i]>max:
max=x[i]
if x[i]>smax and x[i]tmax and x[i]<smax:
tmax=x[i]
print("first max is:-",max)
print("second max is:-",smax)
print("third max is:-",tmax)
#WAP to sort the LIST element in descending order?
ردحذفx=[]
l=int(input("enter the length of list:-"))
for i in range(0,l):
num=int(input("enter the elements:-"))
x.append(num)
print("list is:-",x)
x.sort()
print("descending order",x[::-1])
#list example
ردحذفx=["vegitable","rice","fruits","oil","flour"]
for i in range (0,len(x)):
print(x[i])
for value in x:
print(value)
#list example
ردحذفx=["vegitable","rice","fruits","oil","flour"]
#add element
#x.append("water")
#x.append("pani puri")
#delete element
#del x[2]
#Modify list element
#x[value]=value
for i in range (0,len(x)):
print(x[i])
#particular range
print(x[3:])
print(x[2:4])
print(x[-4:-1])
print(x[-4])
print(x[:3])
print(x[:])
x[3]='maggi'
print(x)
#other example for list delete append modify
ردحذفx=[12,14,120,112,114,420,260]
#x.append(150) #append elements
#x.append(170)
#x.append(190)
#del(x[5]) #delet elements
#del(x[2])
x[0]=120 #modify elements
x[5]=117
for i in range(0,len(x)):
print(x[i])
#We can display Multidimension List using for...in the loop.
ردحذفx=[[1,2,3,4,5],[3,4,5,6,7],[5,6,7,8,9]]
for i in x:
for j in i:
print(i,end='')
print()
#Using List in user value
ردحذفx=[]
size=int(input("number of elements"))
for i in range(0,size):
x.append(input("enter the item"))
for i in range(0,len(x)):
print(x[i])
#All example of List
ردحذفx=[12,23,20,30,45,60,17,70,42,22,14]
print(len(x))
x.append(15)
print(x)
x.remove(60)
print(x)
x.index(20)
print(x.count(17))
y=[1,2,3]
#x=x+y
x.extend(y)
print(x)
x.sort()
print(x)
x.reverse()
print(x)
print(max(x))
print(min(x))
z=(1,2,3)
print(list(z))
3) WAP to divide one List into three different SubList?
ردحذفSol:l=[1,2,3,4,5,6,7,8,9]
l1=[]
l2=[]
l3=[]
for i in l[0:3]:
l1.append(i)
for j in l[3:6]:
l2.append(j)
for k in l[6:9]:
l3.append(k)
l4=[l1,l2,l3]
print(l4)
WAP to divide one List into three different SubList?
ردحذفSol:l=[1,2,3,4,5,6,7,8,9]
l1=[]
l2=[]
l3=[]
for i in l[0:3]:
l1.append(i)
for j in l[3:6]:
l2.append(j)
for k in l[6:9]:
l3.append(k)
l4=[l1,l2,l3]
print(l4)
8) WAP to display repeated elements in LIST but it should display once?
ردحذفSol:-l=[10,20,30,10,20,40,50,40]
l1=[]
for i in l:
s=l.count(i)
if s>1 and i not in l1:
l1.append(i)
print(l1)
1) WAP to calculate the sum of List elements?
ردحذفSol:-l=[1,2,3,4,5,6,7]
s=0
for i in l:
s=s+i
print(s)
4) WAP to reverse LIST elements?
ردحذفSol:-l=[1,2,25,36,45,48,59]
a=len(l)
s=[]
for i in range(a,0,-1):
s.append(l[i-1])
print(s)
5) WAP to combine three lists in one LIST?
ردحذفSol:-l=[1,2,3]
l1=[4,5,6]
l2=[7,8,9]
s=l+l1
d=s+l2
print(d)
7) WAP to display only unique elements in LIST?
ردحذفSol:-l=[10,20,30,10,20,40,50,40]
for i in l:
s=l.count(i)
if s<=1:
print(i)
lst=[1,4,5,8,9]
ردحذفsum=(lst[0]+lst[1]+lst[2]+lst[3]+lst[4])
print(sum)
#find prime number using list?
ردحذفlst=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
for i in range(0,len(lst)):
c=0
for j in range(2,lst[i]):
if lst[i]%j==0:
c=1
break
if c==0:
print("Prime Number is ",lst[i])
else:
print("Not prime",lst[i])
#WAP to combine three lists in one LIST?
ردحذفlst=[1,2,3,4]
lst1=[5,6,7,8]
lst2=[9,10,11,12]
lst3=lst+lst1+lst2
print(lst3)
# WAP to reverse LIST elements?
ردحذفlst=[1,2,3,4,5]
for i in range(len(lst)-1,-1,-1):
print(lst[i],end=" ")
# wap to convert list to stack
ردحذفlst=int(input("Enter number or element"))
lst2=[10,45]
for j in range(0,lst+1):
s=int(input("Enter element"))
lst2.append(s)
print(lst2)
print(lst2)
for i in range(0,len(lst2)):
lst2.pop()
print(lst2)
# wap to convert list to queue
ردحذفlst=int(input("Enter number or element"))
lst2=[10,45]
for j in range(0,lst):
s=int(input("Enter element"))
lst2.append(s)
print(lst2)
for i in range(0,len(lst2)):
lst2.remove(lst2[0])
print(lst2)
#WAP a program to calculate factorial of even element and table of odd number?
ردحذفlst=[2,5,4,7,8]
for i in range(0,len(lst)):
f=1
if lst[i]%2==0:
print("fact ",lst[i])
for j in range(lst[i],0,1):
fact=fact*j
print(fact)
else:
print("Table",lst[i])
for j in range(1,11):
print(lst[i]*j)
#wap factorial of prime numbers and table of odd numbers
ردحذفa=[4,5,6,7,8,9]
for i in range(0,len(a)):
f=1
if a[i]%2==0:
print("fact",a[i])
for j in range(a[i],0,-1):
f=f*j
print(f)
else:
print("table",a[i])
for j in range(1,11):
print(a[i]*j)
#WAP to display only unique elements in LIST?
ردحذفx=[1,2,2,4,5,6,6,22,4,7]
for i in range(0,len(x)):
for j in range(0,len(x)):
if x[i]==x[j] and i!=j:
break
else:
print(x[i])
# WAP to divide one List into three different SubList?
ردحذفx=[12,13,24,56,31,45]
x1=[]
x2=[]
x3=[]
for i in range(0,len(x)):
if i<len(x)//3:
x1.append(x[i])
elif i<len(x)//3+len(x)//3:
x2.append(x[i])
else:
x3.append(x[i])
print(x1,x2,x3)
#wap to calculate the sum of list element ?
ردحذفx=[1,2,3,4]
print(x[0]+x[1]+x[2]+x[3])
#wap to check prime number in list
ردحذفx=[2,3,4,5,6]
for num in (x):
if num>1:
for i in range (2,num):
if num%2==0:
break
else:
print(num)
#wap to divide one list into three different sublist
ردحذفx=[12,23,34,56,76,33]
x1=[]
x2=[]
x3=[]
for i in range(0,len(x)):
if i<len(x)//3:
x1.append (x[i])
elif i<len(x)//3+len(x)//3:
x2.append(x[i])
else:
x3.append (x[i])
print(x1,x2,x3)
#WAP to display repeated elements in LIST but it should display once?
ردحذفx=[4,4,7,8,3,8,6,6]
for i in range (0,len(x)):
for j in range(i+1,len(x)):
if (x[i]==x[j]):
print(x[j])
#wap to find maximum element in list
ردحذفx=[5,1,2,4,3,1]
m=0
sm=0
for i in range(0,len(x)): #x[0]=2,m
if m<x[i]: #x[1]=5<2
m=x[i]
#x[2]=5<4
if sm<x[i] and m!=x[i]: #x[3]=4<6
sm=x[i]
print(m,sm)
#wap to cate marksheet using list with the same condition of if-else:?
ردحذفm=[52,35,64,74,115]
c=0
for i in range(0,len(m)):
if m[i]>=0 and m[i]<=100:
if m[i]<=33:
print("fail")
if m[i]>33:
print("pass")
else:
print("enter only 0 to 100 mark")
#wap to calculate sum of list element ?
ردحذفx=[100,600,6,80]
for i in range(0,len(x)):
print(x[0]+x[1]+x[2]+x[3])
break
#wap to reverse list element ?
ردحذفx=[0,6,8,7]
print(x[3],x[2],x[1],x[0])
#wap to combine three list in one list ?
ردحذفa=[1,2,3]
b=[4,5,6]
c=[7,8,9]
for i in range(0,len(a)):
for j in range(0,len(b)):
for k in range(0,len(c)):
continue
print(a+b+c)
#wap to reverse list elements ?
ردحذفx=[4,3,2,1]
for i in range(len(x)-1,-1,-1):
print(x[i])
#prime elemnt tuple in list?
ردحذفa=([1,2,3,4,5],(3,4),[7,8],[1,2,3,4,5])
for i in a:
for j in i:
for k in range(2,j):
if j%k==0:
break
else:
if j>1:
print("prime number","","=",j)
print()
#wap to additional matrix ?
ردحذفa=[1,2] #4,6
b=[3,4]
for i in a,b:
print("additonal matrix","=",a[0]+b[0],",",a[1]+b[1])
break
#dout:--sir is this correct ???
#wap to find mas,smax,in list
ردحذفx=[2,5,8,9,7,6]
m=0
s=0
t=0
for i in range (0,len(x)):
if m<x[i]:
t=s
s=m
m=x[i]
elif s<x[i]:
t=s
s=x[i]
elif t<x[i]:
t=x[i]
print(m,s,t)
#wap to multiply the matrix
ردحذفx=[[1,2],[4,5]]
y=[[2,4],[1,2]]
z=[[int]*2,[int]*2]
for i in range(0,2):
for j in range(0,2):
s=0
for k in range (0,2):
s=s+x[i][k]*y[k][i]
z[i][j]=s
print(s)
#wap to find prime number in list
ردحذفx=[2,5,23,13,14,25,45]
for num in x:
if num>1:
for x in range(2,len(x)):
if (num%2==0):
break
else:
print("prime numbers")
#wap to reverse the list elements
ردحذفx=[1,8,5,6]
y=[]
for i in range(len(x)-1,-1,-1):
y.append(x[i])
print(x[i])
print(x)
print(y)
#wap to decreasing order of list element
ردحذفa=[12,14,25,35,11,18]
print("befor sort",a)
a.sort()
a.reverse()
print("after sort",a)
print("after reverse",a)
for element in a:
print(element)
#wap to sorting in list
ردحذفx=[12,25,85,32,14]
for i in range (0,len(x)):
for j in range(i+1,len(x)):
if x[i]>x[j]:
x[i],x[j]=x[j],x[i]
print(x)
#wap to sum of list element
ردحذفx=[1,2,3,4]
s=0
for i in range(0,4):
s=s+x[i];
print(s)
#wap to unique element
ردحذفx=[1,3,4,5,]
result=[]
for i in x:
if i not in result:
result.append(i)
print(result)
#wap to convert nested list to single list
ردحذفI=[1,2,[3,4,5,[6,7]]]
A=I[0:2]
B=I[2:]
C=[]
D=[]
for i in B:
for j in i:
if type.__name__=="int":
C.append(j)
else:
for k in j:
D.append(k)
list=A+C+D
print(list)
#prime no using list
ردحذفa=[1,2,3,4,4,5,11]
for i in a:
if i%2!=0:
print (i)
BHAVANI SINGH THAKUR
# write a program to display prime elements in list?
ردحذفx=[1,2,4,3,7,6,5,9,13,11,15]
for num in x:
if num>1:
for i in range(2,num):
if num%i==0:
break
else:
print(num)
#wap to find max element in list?
ردحذفnum=[1,3,8,13,9,5,]
m=num[0]
for i in range(0,len(num)):
if m<num[i]:
m=num[i]
print("maximum ele is {0}".format(m))
# list examples:-
ردحذفx=["abhi","avi","vini","anvi"]
for i in range(0,len(x)):
print(x[i])
x.append("mahi") #add element
print(x)
del x[0] # delete element
print(x)
x[0]='vani' # modify ele
print(x)
for i in range(len(x)-1,-1,-1): # reverse
print(x[i])
# wap to arrange list elements?
ردحذفx=[1,2,3,1,1,5,2,3,4,5]
for i in range(0,len(x)):
x.sort()
print(x)
#muitidiamention list using for in the loop?
ردحذفx=[[1,2,3],[4,5,6],[7,8,9]]
for i in x:
for j in i:
print(j,end="")
print()
# wap to calculate the sum of list elements?
ردحذفs=0
x=[2,4,6,8]
for i in range(0,len(x)):
s=s+x[i]
print("sum of ele",s)
#wap to divided one list into three different sublist?
ردحذفx=["oil","rice","maggi","fruit","vegetable"]
size1=len(x)//3
size2=len(x)//3
size3=len(x)-(size1+size2)
print(x[0:size1])
print(x[size1:size1+size2])
print(x[size1+size2:size1+size2+size3])
#wap to reverse list elements?
ردحذفa=[10,20,30,40,50]
for i in range(len(a)-1,-1,-1):
print(a[i])
#wap to combine three list in one list?
ردحذفp=[11,12,13]
q=[14,15,16]
r=[17,18,19]
s=p+q+r
print("combine elements is",s)
#wap to combine three list in one list?
ردحذفp=[11,12,13]
q=[14,15,16]
r=[17,18,19]
s=p+q+r
print("combine elements is",s)
# wap to display oniy unique elements in list?
ردحذفx=[23,45,23,16,11,16,78,45,90]
y=[]
for i in x:
if i not in y:
y.append(i)
print(y)
# wap to display oniy unique elements in list?
ردحذفx=[23,45,23,16,11,16,78,45,90]
y=[]
repeat=[]
for i in x:
if i not in y:
y.append(i)
else:
repeat.append(i)
print("repeat ele is",repeat)
# WAP to Display Prime elements in List
ردحذفnum = int(input('Enter the length of list : '))
l = []
count =0
for i in range(0,num):
n=int(input())
l.append(n)
for i in range(0,num):
for j in range(2,l[i]):
if l[i] % j == 0 :
count=1
break
if count == 0 :
print(l[i])
count=0
# WAP to find max elements in list
ردحذفl=[]
n = int(input("ENTER THE LENGHT OF LIST : "))
for i in range(0,n) :
l.append(int(input()))
max = l[0]
for i in range(1,n):
if l[i]>max:
max = l[i]
print("maximum is ",max)
# WAP to sort elements in list
ردحذفl=[]
n = int(input("ENTER THE LENGHT OF LIST : "))
for i in range(0,n) :
l.append(int(input()))
l.sort()
print(l)
# WAP to calculate the sum of List elements
ردحذفn = int(input("Enter length of the list : "))
l = []
sum=0
for i in range(0,n):
num = int(input())
l.append(num)
sum+=num
print("Sum of the elements of the list is : ",sum)
# WAP to display prime elements in LIST
ردحذفn = int(input("Enter length of the list : "))
l = []
count=0
for i in range(0,n):
num = int(input())
l.append(num)
for i in range(0,n):
for j in range(2,l[i]) :
if l[i]% j ==0:
count=1
break
if count == 0 :
print(l[i])
count=0
# WAP to display prime elements in LIST
ردحذفn = int(input("Enter length of the list : "))
l = []
count=0
for i in range(0,n):
num = int(input())
l.append(num)
for i in range(0,n):
for j in range(2,l[i]) :
if l[i]% j ==0:
count=1
break
if count == 0 :
print(l[i])
count=0