List Operation in Python:-
The list is a collection of items using proper sequence, It is similar to an array of another programming languages but the array contains a collection of similar type of elements but list contains a collection of the similar and dissimilar type 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:-
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]
Modify List element:-
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])
all arithmetic operation
Addition and multiplication using LIST:-a= [1,2,3]
b = [4,5,6]
Addition:-
c=a+b + is used to combine two list elements in one list
[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:-
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:-
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()
How to take input from Users in LIST?
for i in range(0,2):
for j in range(0,3):
print(x[i][j],end=' ')
print()
How to take input from Users in LIST?
In most of 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])
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 marksheet using LIST with the same condition of IF-ELSE?
7) WAP to display only unique element 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?
Lokesh Rathore
ReplyDelete#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?
ReplyDeletex=[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?
ReplyDeletesum=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
ReplyDeletex=[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?
ReplyDeletex = ["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?
ReplyDeletea=[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?
ReplyDeleteans:
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?
ReplyDeleteSolution:-
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 ?
ReplyDeleteSolution:-
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?
ReplyDeletex = [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?
ReplyDeletex = [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--->>
ReplyDeletetotal=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-->>
ReplyDeletestart=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-->>
ReplyDeleteu=["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:-
ReplyDeletex = [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-->>
ReplyDeletex = [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--->>>
ReplyDeletep=["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-->>
ReplyDeletep=["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
ReplyDeletecount=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?
ReplyDeletelist=[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
ReplyDeletelist=[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-->>
ReplyDeletelist1= [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]
ReplyDeletefor 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]
ReplyDeletem=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-->>
ReplyDeletex=[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 ??
ReplyDeletex = [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 ??
ReplyDeletep=["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 ??
ReplyDeletex=[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 ??
ReplyDeletelist=[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 ??
ReplyDeletex = [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"]
ReplyDeletemarks=[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-->>
ReplyDeletex=[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?
ReplyDeletex=[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
ReplyDeletex=[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:
ReplyDeletex=[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?
ReplyDeletey =[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?
ReplyDeletex=[22,11,33]
y=[55,44,66]
z=[77,66,99]
a=x+y+z
print(a)
lst = []
ReplyDeletenum = 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
Deletestart = 100
ReplyDeleteend = 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
Delete# Python code to split a list
ReplyDelete# 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
DeletestudentNames = ["abcd", "efgh", "ijkl", "mnop"]
ReplyDeletestudentNames.reverse()
print(studentNames)
Without using reverse()
Deletelist1 = [1,2,3,4]
ReplyDeletelist2 = [5,6,7,8]
list3 = [9,10,11,12]
list1 = list1 + list2 + list3
print("the modified list is " + str(list1))
Without using +
DeleteABHISHEK GOYAL
ReplyDeleteWAP 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
ReplyDelete# 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
ReplyDeleteWAP 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
ReplyDeletex = [34,7625,9876,19]
print(x[::-1])
#WAP to divide one List into three different SubList?
ReplyDeletex = [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?
ReplyDeletem= []
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
ReplyDelete#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
ReplyDeleteWAP 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)
ReplyDelete# 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)
ReplyDelete# 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)
ReplyDelete# 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)
ReplyDelete# 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)
ReplyDelete#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 )
ReplyDelete# 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)
ReplyDelete# 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)
ReplyDelete# 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)
ReplyDelete# 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)
ReplyDelete# 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)
ReplyDelete# 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:
ReplyDeletelist=[2,27,22,72,77,7]
sum=0
for i in list:
sum=sum+i
print(sum)
#Reverse_Every_List_Element
ReplyDeleterev=[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
ReplyDeletefun=[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
ReplyDeletelist=[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
ReplyDeletelist=[1,1,2,7,9,9]
unique=[]
for i in list:
y=list.count(i)
if y==1:
unique.append(i)
print(unique)
#DuplicateElementsInList
ReplyDeletel=[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
ReplyDeletelist=[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"
ReplyDeletex = [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 ?
ReplyDelete#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?
ReplyDelete#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?
ReplyDeletea=[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"
ReplyDeleteL = [34,7625,9876,19]
print(L[::-1])
#Reverce List Iteam "By Surendra"
ReplyDeleteL = [34,7625,9876,19]
print(L[::-1])
#Reverce List Iteam "By Surendra"
ReplyDeleteL = [34,7625,9876,19]
print(L[::-1])
# WAP to reverse LIST elements? #
ReplyDelete# 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?
ReplyDeletea=[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
ReplyDelete#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)
Post a Comment
If you have any doubt in programming or join online classes then you can contact us by comment .