Programs on List Manipulation

Q1 Write a Python program to get the smallest number from a list

Items=list(input(“Enter List Elements “))

min = items[ 0 ]

for a in items:

     if a < min:

         min = a

print(“Minimum NO. IN LIST “,min)

Q2 Write a Python program to sum all the items in a list.

Items=list(input(“Enter ListElements “))

    sum_numbers = 0

    for x in items:

        sum_numbers += x

print(“Sum of List elements “,sum_numers))

Q3 Write a Python program to get the largest number from a list.

Items=list(input(“Enter List Elements “))

max = items[ 0 ]

for a in items:

    if a > max:

        max = a

print(“MAXIMUM NO. IN LIST “,max)

Q4. Write a Python program to remove duplicates from a list

a = [10,20,30,20,10,50,60,40,80,50,40]

dup_items = set()

uniq_items = []

for x in a:

    if x not in dup_items:

        uniq_items.append(x)

        dup_items.add(x)

print(dup_items)

Q5.Write a Python program to check a list is empty or not

l = []

if not l:

     print(“List is empty”)

Q6.

“”” Program to count numbers in a List”””

x = [4, 7, 9, 12, 10]
count = 0

for i in x:

count= count + 1

print(“Total number of elements = “, count)

7.

“”” Program to find average of numbers in a List”””

A = [6, 2, 7, 9, 1, 3]

Sum = 0

Avg = 0

for x in range(len(A)):

Sum += A[x];

Avg = Sum/len(A);

print(“Average = “, Avg)

8.

“” Program to find maximum among N numbers in a List”””

x = []

N = eval(input(“entersize of list : “))

for i in range(0, N):

x.append(eval(input(“enter”+ str(i) + ” element : “)))
print(“Numbers in the list are “)

print(x)

max = x[0]

for i in range(1, N):

if ( x[i] > max): max = x[i]

print(“Maximum value in the list = “, max)

9.

“” Program to find minimum among N numbers in a List”””

x = []

N = eval(input(“entersize of list : “))

for i in range(N)

x.append(eval(input(“enter”+ str(i) + ” element : “)))

print(“Numbers in the list are “)

print(x)

min = x[0]

for i in range(1, N):

if ( x[i] < min):

min = x[i]

print(“Minimum value in the list = “, min)

10.

“”” Program to search a number in the list (Linear Search) “””

A=[]

N=eval(input(“entertotal numbers to be entered”)) for i in range(0,N):

A.append(eval(input(“Enter”+str(i)+”Element”)))

s=eval(input(“enterelement to be searched”)) found=-1

for i in range(0,N):

if (s==A[i]):

found=i

if (found!=-1):

print(“element found at position”,found) else:

print(“element not found”)

11.

“”” Program to arrange numbers in ascending order using Bubble Sort Method”””

x = []

N = eval(input(“entersize of list : “))

for i in range(0, N):

x.append(eval(input(“enter”+ str(i) + ” element : “)))

print(x)

count = 0

for i in range(0, N-1):

for j in range(0, N-i-1):

if x[j] > x[j+1]:

x[j], x[j+1]=x[j+1], x[j]

count = count + 1

print(“Pass”,i+1,”:”, x)

print(“Total number of operations = “, count)

print(“Elements in ascending order are : \n”, x)

12. “”” Program to arrange numbers in ascending order using Insertion Sort Method”””

x = []

N = eval(input(“entersize of list : “))

for i in range(0, N):

x.append(eval(input(“enter”+ str(i) + ” element : “)))

print(“List with original elements :\n”,x)

#we assume that smallest element at first index i.e. 0

for i in range(2, N):

t = x[i]

k = i-1

while t < x[k]:

x[k+1] = x[k]

k = k – 1

x[k+1] = t

print(“Sorted List in ascending order :\n”,x)

13.

“”” Program to arrange numbers in ascending order using Selection Sort Method”””

x = []

N = eval(input(“entersize of list : “)) for i in range(0, N):

x.append(eval(input(“enter”+ str(i) + ” element : “))) print(x)

count = 0

for i in range(0, N-1):

for j in range(i+1, N):

if x[i] > x[j]:

x[i], x[j] = x[j] x[i]

count = count + 1

print(“Pass”,i+1,”:”, x)

print(“Total number of operations = “, count)

print(“Elements in ascending order are : \n”, x)

14. Program to read a list and print only those numbrs which are divisible by 5 and not by 7

num=list(eval((input(“Enter numbers “))))
for i in num:
if (i%5==0 and i%7 !=0):
print (i,”is divisable by 5 but not by 7″)

15. Program to read String and check whether it is palindrome using list

msg=input(“Enter any string : “)
newlist=[]
newlist[:0]=msg
l=len(newlist)
last=l-1
for i in range(0,l):
if newlist[i]!=newlist[last]:
print (“Given String is not a palindrome”)
break
if i>=last:
print (“Given String is a palindrome”)
break
l=l-1
last = last- 1

Advertisement