List Programs
M3-R5.1 · Chapter 5: Sequence Data Types · 10 min read
List Manipulation
Q1. Write a program to increment the elements of a list with a number.
lst = [ ]
while True :
num = int(input("Enter a number :- "))
lst.append(num)
user = input("Do you want to quit enter yes =")
if user == "yes":
print(lst)
break
Q2. Write a program that reverses a list of integers (in place).
lst = eval(input("Enter a list :-"))
lst.reverse()
print(lst)
Q 3. Write a program that input two lists and create a third, that contains all elements of the first
followed by all elements of the second.
a = eval(input ("Enter the first list ="))
b = eval (input ("Enter the second list ="))
print ("New list = ", a+b)
Q4. Ask the user to enter a list containing numbers between 1 and 12 then replace all of the
entries in the list that are greater than 10 with 10.
a = eval(input ("Enter the list ="))
for i in range(len(a)) :
if a[i] > 10 :
a[i] = 10
print ("New list ",a)
OR By List Comprehension Method
a = eval(input ("Enter the list ="))
newlist = [ x if x <= 10 else 10 for x in a ]
print("New list ",newlist)
Output :-
Enter the list =[9,12,15,2,3,18]
New list [9, 10, 10, 2, 3, 10]
>>>
Enter the list =[78,5,2,98,41,35,2,8,1,10]
New list [10, 5, 2, 10, 10, 10, 2, 8, 1, 10]
>>>
Q5. Ask the user to enter a list of string. Create a new list that consist of those strings with there
first characters removed.
st = eval(input ("Enter the list of string = "))
for i in range(len(lst)) :
lst [ i ] = lst [ i ] [ 1:]
print("New list = ",lst)
Output :-
Enter the list of string = ["Shiv","Kashi","Portal","Express"]
New list = ['hiv', 'ashi', 'ortal', 'xpress']
>>>
Enter the list of string = ["Python","C++","Java","CSS"]
New list = ['ython', '++', 'ava', 'SS']
Q6. Write a program to check if a number is present in the list or not. If the number is present, print
the position of the number.
Print an appropriate message if the number is not present in the list.
lst = eval(input("Enter first list :-"))
num = int(input("Enter the number which you want to search :-"))
if num in lst :
print(lst.index( num ) )
else :
print("Number not found")
Q7. Crate the following lists using a for loop:
(a) A list consisting of the integers 0 through 49.
(b) A list consisting the square of the integer 1 through 50
(c) The list [“a”, “bb”, “ccc” , “dddd” ..........] that ends with 26 copies of the letter z.
lst = [ ]
for i in range(50):
lst = lst + [ i ]
print(lst)
B)
lst = [ ]
for i in range(51):
lst = lst + [ i**2 ]
print(lst)
C)
lst = [ ]
for i in range(1,27):
lst = lst + [ chr(96+i) * i ]
print(lst)
Q8. Write a program that takes any two list l and m of the same size and adds their elements
together to form a new list n whose element are sum of the corresponding elements in l and m.
l = eval(input("Enter first list = "))
m = eval(input("Enter second list = "))
n = [ ]
for i in range(len(l)):
n = n + [ l [ i ] + m [ i ] ]
print("New list = ",n)
Output :-
Enter first list = [1,2,3,4,5,6,7,8,9]
Enter second list = [1,2,3,4,5,6,7,8,9]
New list = [2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>
Enter first list = [9,8,7,6,5,4,3,2,1]
Enter second list = [1,2,3,4,5,6,7,8,9]
New list = [10, 10, 10, 10, 10, 10, 10, 10, 10]
Q9. Write a program rotates the elements of a list show that the element at the first index moves to
the second index ,the element i n second index moves to the third index, etc, and the element in the
last index moves to the first index.
lst = eval(input("enter the list = "))
print ("New list =", [lst[ -1] ] + lst[0 : -1] )
Output :-
Enter the list :- [3, 5, 6, 9, 10, 12, 15, 18, 20, 21]
New list = [21, 3, 5, 6, 9, 10, 12, 15, 18, 20]
Enter the list :- ["shiv","kashi","Portal","Express"]
New list = ['Express', 'Shiv', 'Kashi', 'Portal']
>>
Enter the list :- ["C++","Python","Java","CSS"]
New list = ['CSS', 'C++', 'Python', 'Java']
Q10. Write a program that reads the n to display 9th term of Fibonacci series: the Fibonacci
sequence work as follows:
0,1,1,2,3,5,8,13,21,34,...
term = int (input ("Enter the number ="))
fib = [0,1,1]
for i in range(term - 2):
fib = fib + [ fib [ i + 1] + fib [ i + 2 ] ]
print("Fibonacci sequence :-",fib)
print( fib [ term ] )
Q11. Write a program as per following specifications:
(i) return the length of the longest String in the list of a strings str_list. Precondition: the list will
contain At least one element.
(ii) L is a list of numbers. return a new list where each element is the corresponding element of list L
summed with number num.
(i)
lst = eval(input("Enter a list of string = "))
if lst == [] :
print("List should contain at least one element ")
else :
long = ""
for i in range(len(lst)) :
if len(long ) < len(lst[i]) :
long = lst[i]
print("Longest string in list = ",long)
(ii)
lst = eval(input("Enter a list = "))
num = int(input("Enter a number = "))
for i in range(len(lst)):
lst[i] = lst[i] + num
print("New list = ",lst)
Output :-
(i)
Enter a list of string = ["Shiv","Kashi","Portal","Express"]
Longest string in list = Express
>>>
Enter a list of string = []
List should contain at least one element
>>>
(ii)
Enter a list = [1,2,3,4,5,6,7,8,9]
Enter a number = 5
New list = [6, 7, 8, 9, 10, 11, 12, 13, 14]
>>>
Enter a list = [78,54,2,9,4,6,3,84,21,74]
Enter a number = 9
New list = [87, 63, 11, 18, 13, 15, 12, 93, 30, 83]
Q12. Write a program to read two lists num and denum which contain the numerators and
denominators of same fractions at the respective indexes. Then display the smallest fraction
along with its index.
num = eval(input("Enter numerators list :-"))
denum = eval(input("Enter denominators list :-"))
lst = [ ]
for i in range(len(num)) :
fraction = num[ i ] / denum[ i ]
lst.append( fraction )
lst.sort()
print("Smallest fraction = ",lst[ 0 ])
Output : -
Enter numerators list :-[0,2,4,6,8]
Enter denominators list :-[1,3,5,7,9]
Smallest fraction = 0.0
>>>
Enter numerators list :-[98,45,23,47]
Enter denumerators list :-[54,26,89,74]
Smallest fraction = 0.25842696629213485
Q13. Write a program to display the maximum and minimum values from the specified range of
indexes of a list.
lst = eval(input("Enter a list :-"))
start = int(input("Enter starting index :-"))
stop = int(input("Enter stoping index :-"))
print( "Max :-", max( lst [ start : stop ] ) )
print("Min :- ", min( lst [ start : stop ] ))
Q14. Write a program to move all duplicate values in a list to the end of the list.
lst = eval(input("Enter a list :-"))
dup = [ ]
n = 1
while n < len(lst):
for i in lst :
if lst.count( i ) != 1 :
lst.remove( i )
dup.append(i)
n += 1
lst.sort()
print("New list :- ",lst + dup)
Q15. Write a program to compare two equal sized lists and print the first index where they differ.
lst1 = eval(input("Enter first list :-"))
lst2 = eval(input("Enter second list :-"))
for i in range(len(lst1)):
if lst1[i] != lst2[i] :
print("index = ",i)
break
Output: Enter first list :-[1,2,3,4,5,6,7,8,9]
Enter second list :-[1,3,2,4,5,6,7,8,9]
index = 1