0% found this document useful (0 votes)
15 views

List Assignment Solution

The document contains Python code snippets for performing various operations on lists such as sorting, counting elements, finding average, sum, even/odd elements etc. It also includes examples of suppressing elements based on conditions and extracting elements matching certain criteria.

Uploaded by

lucifergamer996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

List Assignment Solution

The document contains Python code snippets for performing various operations on lists such as sorting, counting elements, finding average, sum, even/odd elements etc. It also includes examples of suppressing elements based on conditions and extracting elements matching certain criteria.

Uploaded by

lucifergamer996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

# 3 Arrange the elements of a list in increasing order of their value.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
l.sort()
print(l)

# 4 Arrange the elements of a list in decreasing order of their value.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
l.sort(reverse=True)
print(l)

# 5 Read a number from the keyboard and check whether the entered number is present or not in the list. If present,
then also print the occurrences of a entered number.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
el=int(input("Enter an Element "))
count=l.count(el)
if count==0:
print("Element Not Present in list")
else:
print("Element Present",count," Times")

# 6 Find out the average of n elements.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
print("Average =",sum(l)/n)
# [7] Find out the sum of each elements in the list.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
print("Sum =",sum(l))

# [8] Find out the total number of even elements in the list.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
even=0
for i in l:
if i%2==0:
even+=1
print("Total Even Element = ",even)
# [9] Find out the total number of odd elements in the list.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
odd=0
for i in l:
if i%2!=0:
even+=1
print("Total Odd Element = ",odd)

# [10] Suppress all zero elements at the bottom of the list.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
zero=[]
nzero=[]
for i in l:
if i==0:
zero.append(i)
else:
nzero.append(i)
print(nzero+zero)

# [11] Suppress all negative elements at the bottom of the list.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
neg=[]
pos=[]
for i in l:
if i<0:
neg.append(i)
else:
pos.append(i)
print(pos+neg)

# [12] Suppress all positive elements at the bottom of the list.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
neg=[]
pos=[]
for i in l:
if i<0:
neg.append(i)
else:
pos.append(i)
print(neg+pos)

# [13] Suppress all non-zero elements at the bottom of the list.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
zero=[]
nzero=[]
for i in l:
if i==0:
zero.append(i)
else:
nzero.append(i)
print(zero+ nzero)

# [14] Return the largest even number from the list, if there no even number in the input, print “Even not found in the
list”.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
even=[]
for i in l:
if i%2==0:
even.append(i)
if len(even)==0:
print("No even element present in list")
else:
print("Max Even Number =",max(even))

[15]. Write a Program in python to add all those values in the list of SCORES, which are ending with zero (0) and display
the sum.
For example:
If the SCORES contain [200, 456, 300, 100, 234, 678]
The sum should be displayed as 600
score=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
score.append(a)
print(score)
sum=0
for i in score:
if i%10==0:
sum=sum+i
print("Sum =",sum)

[16]. Write a Program in python to find and display those place names, in which there are more than 5 characters.
For example:
If the list PLACES contains
["DELHI","LONDON","PARIS","NEW YORK","DUBAI"]
The following should get displayed:
LONDON
NEW YORK
place=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=input("Enter a Place")
place.append(a)
print(place)
for i in place:
if len(i)>5:
print(i)

[17]. Write Program in python to add those values in the list of NUMBERS, which are odd.

l=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=int(input("Enter a Number "))
l.append(a)
print(l)
odd=0
for i in l:
if i%2!=0:
odd=odd+i
print(“Sum of Odd Numbers = “,odd)

[18]. Write Program in python to display all the state names from a list of STATES, which are star ng with alphabet M.
For example:
If the list STATES contains
["MP","UP","WB","TN","MH","MZ","DL","BH","RJ","HR"]
The following should get displayed:
MP
MH
MZ
states=[]
n=int(input("How Many Elements You want to add "))
for i in range(n):
a=input("Enter a state")
states.append(a)
print(states)

for i in states:
if i[0]=='M':
print(i)
[19]. Write a PROGRAM in Python to find and display the extra element in List A. List A contains all the elements of List
B but one more element extra. (Restric on: array elements are not in order)
Example If the elements of List A is 14, 21, 5, 19, 8, 4, 23, 11
and the elements of List B is 23, 8, 19, 4, 14, 11, 5
Then output will be 21

A=[14, 21, 5, 19, 8, 4, 23, 11]


B=[23, 8, 19, 4, 14, 11, 5]
for i in A:
if i not in B:
print(i," Not Present in List B")

You might also like