**
List Operation in Python:-**

The list is a collection of items using proper sequence, It is similar to an array of another programming language.

The list provides an ordered collection of data, and it can store any type of elements.

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

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

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 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=' ')

## Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .