Professional Documents
Culture Documents
In [1]: L1=['a','b','c','d']
L1.append('e')
print(L1)
4.Write a program to find the second largest element in the list ‘Num’.
In [11]: L1=[0,1,1,2,3,5,8,13,21,34,55,89,144]
a=int(input("enter the element to be checked:"))
print("the number of occurences of:", a, "is",L1.count(a))
6.Write a program to read a list of elements input elements and position from the user
that has to be inserted in the list.
In [18]: L1 = [1,2,3,4,5]
a = int(input("Enter element to be inserted:"))
b = int(input("index of the inserted element:"))
L1.insert(b,a)
print(L1)
In [40]: L1 = [1,10,25,12,-10,37,49,71,109]
L1.sort()
print("The largest element is",L1[-1])
9.Write a program to put the even and odd elements in a list into two different lists.
In [42]: L1 = [5,3,2,47,29,16,30]
odd=[]
even=[]
for i in L1:
if i>1 and i%2==0:
even.append(i)
else:
odd.append(i)
print("The even elements in the list are", even)
print("The odd elements in the list are", odd)
In [43]: L1 = [1,7,11,2,9,3]
L2 = [27,31,19,16,12,6]
L1.extend(L2)
L1.sort()
print(L1)
In [44]: L1 = [2,3,6,9,12,29,37,26,42]
sum = 0
for i in L1:
if i>1 and i%3==0:
sum+=i
print("The sum of the multiples of 3 is:",sum)
12.Write a program which takes list from the user and multiply each and every
element of list by 2.
In [47]: L1 = [1,3,5,7,9]
L2=[]
for i in L1:
i*=2
L2.append(i)
print("The multiplied list is:",L2)
In [48]: L1 = [1,2,4,6,10,29,37]
sum = 0
for i in L1:
sum+=i
print("The average is:", sum/len(L1))
1. Append
2. Insert
3. Append a list to another list
4. Modify element
5. Delete existing element from given postion
6. Delete existing element with given value
In [55]: my_list = []
choice = 0
while choice != 7:
print("Menu:")
print("1. Append an element to the list")
print("2. Insert an element at a specific position")
print("3. Append a list to another list")
print("4. Modify an element in the list")
print("5. Delete an element from a specific position")
print("6. Delete an element with a specific value")
print("7. Exit")
print("Updated list:",my_list)
Menu:
1. Append an element to the list
2. Insert an element at a specific position
3. Append a list to another list
4. Modify an element in the list
5. Delete an element from a specific position
6. Delete an element with a specific value
7. Exit
Enter your choice: 3
Enter a list of elements to append (space-separated): 13579
Updated list: ['1', '3', '5', '7', '9']
Menu:
1. Append an element to the list
2. Insert an element at a specific position
3. Append a list to another list
4. Modify an element in the list
5. Delete an element from a specific position
6. Delete an element with a specific value
7. Exit
Enter your choice: 2
Enter the element to insert: 27
Enter the position to insert: 5
Updated list: ['1', '3', '5', '7', '9', '27']
Menu:
1. Append an element to the list
2. Insert an element at a specific position
3. Append a list to another list
4. Modify an element in the list
5. Delete an element from a specific position
6. Delete an element with a specific value
7. Exit
Enter your choice: 4
Enter the position of the element to modify: 4
Enter the new element: 32
Updated list: ['1', '3', '5', '7', '32', '27']
Menu:
1. Append an element to the list
2. Insert an element at a specific position
3. Append a list to another list
4. Modify an element in the list
5. Delete an element from a specific position
6. Delete an element with a specific value
7. Exit
Enter your choice: 6
Enter the value of the element to delete:27
Updated list: ['1', '3', '5', '7', '32']
Menu:
1. Append an element to the list
2. Insert an element at a specific position
3. Append a list to another list
4. Modify an element in the list
5. Delete an element from a specific position
6. Delete an element with a specific value
7. Exit
Enter your choice: 5
Enter the position of the element to delete:2
Updated list: ['1', '3', '7', '32']
Menu:
1. Append an element to the list
2. Insert an element at a specific position
3. Append a list to another list
4. Modify an element in the list
5. Delete an element from a specific position
6. Delete an element with a specific value
7. Exit
Enter your choice: 1
Enter the element to append: 2
Updated list: ['1', '3', '7', '32', '2']
Menu:
1. Append an element to the list
2. Insert an element at a specific position
3. Append a list to another list
4. Modify an element in the list
5. Delete an element from a specific position
6. Delete an element with a specific value
7. Exit
Enter your choice: 8
Invalid choice!
Updated list: ['1', '3', '7', '32', '2']
Menu:
1. Append an element to the list
2. Insert an element at a specific position
3. Append a list to another list
4. Modify an element in the list
5. Delete an element from a specific position
6. Delete an element with a specific value
7. Exit
Enter your choice: 7
Exiting...
Updated list: ['1', '3', '7', '32', '2']
In [56]: #(a)
list=['p','r','o','b','l','e',',m']
list[1:3]=[]
print(list)
list[2:5]=[]
print(list)
In [57]: #(b)
L1 =[13, 18, 11, 16, 13, 18, 13]
print(L1. index(18))
print(L1 .count(18))
L1.append(L1.count(13))
print(L1)
1
2
[13, 18, 11, 16, 13, 18, 13, 3]
In [58]: #(c)
T1=[1,3,5,6,]
T1[0]=2
print(T1)
[2, 3, 5, 6]
In [59]: #(d)
X = [4, 7, 9, 12, 10]
count = 0
for i in X:
count= count+1
print("Total number of elements = ", count)
Total number of elements = 5
In [60]: #(e)
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("Sum = ",Sum)
print("Average = ", Avg)
Sum = 28
Average = 4
In [61]: #(f)
x =[]
N = eval(input("enter size of list: "))
for i in range(0, N):
x.append(eval(input("enter "+ str( i) + " element : ")))
print("Numbers in the list are ")
print(x)
max1 = x[0]
for i in range(1, N):
if ( x[i] > max1 ):
max1 = x[i]
print("Maximum value in the list = ", max1)