Type C list
Type C list
Solution
for i in range(len(lst)):
lst[i] += n
Output
Solution
Output
Question 3
Write a program that inputs two lists and creates a third, that contains all elements of the first followed by all
elements of the second.
Solution
Output
Solution
for i in range(len(l)):
if l[i] > 10:
l[i] = 10
Output
Enter list having numbers between 1 & 12: [1, 3, 15, 8, 20]
List after removing numbers greater than 10:
[1, 3, 10, 8, 10]
Question 5 Ask the user to enter a list of strings. Create a new list that consists of those strings with their
first characters removed.
Solution
for i in range(len(l1)):
l2.append(l1[i][1:])
Output
if n in l:
print(n, "found at index", l.index(n))
else :
print(n, "not found in list")
Output
=====================================
Solution
l = []
for i in range(50):
l.append(i)
Output
Question 7b
Solution
l = []
Output
Solution
l = []
print("Created List:")
print(l)
Output
Created List:
['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii',
'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn',
'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr',
'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu',
'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx',
'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']
Question 8
Write a program that takes any two lists L and M of the same size and adds their elements together to form a
new list N whose elements are sums of the corresponding elements in L and M. For instance, if L = [3, 1, 4]
and M = [1, 5, 9], then N should equal [4,6,13].
Solution
for i in range(len(L)):
N.append(L[i] + M[i])
print("List N:")
print(N)
Output
Question 9
Write a program rotates the elements of a list so that the element at the first index moves to the second
index, the element in the second index moves to the third index, etc., and the element in the last index moves
to the first index.
Solution
l = l[-1:] + l[:-1]
print("Rotated List")
print(l)
Output
Write a program that reads the n to display nth term of Fibonacci series.
The program prompts for element and prints out the value of that element of the Fibonacci sequence.
Thus:
• input 7, produces 13
• input 9, produces 34
Hints:
A Don't try to just type out the entire list. It gets big very fast. Element 25 is 75205. Element 100 is
354224848179261915075. So keep upper limit of n to 20.
Solution
n = int(input("Enter n: "))
if (n > 20):
print("n should be less than or equal to 20")
else :
a = 0
b = 1
c = a + b
for i in range(3, n + 1):
a = b
b = c
c = a + b
Output
Enter n: 7
7 term of Fibonacci series = 13
=====================================
Enter n: 9
9 term of Fibonacci series = 34
=====================================
Enter n: 25
n should be less than or equal to 20
Question 11a
Solution
for i in range(len(l)):
length = len(l[i])
if length > largeLen:
largeLen = length
largeIdx = i
Output
Question 11b
'''L is a list of numbers. Print a new list where each element is the corresponding element of list L summed
with number num.'''
Solution
l2 = []
for i in l1:
l2.append(i + num)
print("New list:")
print(l2)
Output
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.
Solution
small = 0.0
smallIdx = 0
for i in range(len(num)):
t = num[i] / denum[i]
if t < small:
small = t
smallIdx = i
Output
Question 13
Write a program to display the maximum and minimum values from the specified range of indexes of list.
Solution
Output
Enter the list: [89, 42, 12, 56, 35, 2, 8, 7, 13, 69]
Enter start index: 3
Enter stop index: 8
Maximum = 56
Minimum = 2
Question 14
Write a program to move all duplicate values in a list to the end of the list.
Solution
l = dedup + dup
print("Modified List:")
print(l)
Output
Enter the list: [20, 15, 18, 15, 7, 18, 12, 13, 7]
Modified List:
[20, 15, 18, 7, 12, 13, 15, 18, 7]
Question 15
Write a program to compare two equal sized lists and print the first index where they differ.
Solution
for i in range(len(l1)):
if l1[i] != l2[i]:
print("Lists differ at index", i)
break;
else:
print("Lists are equal")
Output
=====================================