You are on page 1of 7

Lists Assignment

By Daksh Sachdev XI-A

1.Write a program to add elements to a list.

In [1]: L1=['a','b','c','d']
L1.append('e')
print(L1)

['a', 'b', 'c', 'd', 'e']

2. Write a program to add list to a list.

In [3]: L1= [1,2,3,4,5]


L2=['a','b','c']
L1.extend(L2)
print(L1)

[1, 2, 3, 4, 5, 'a', 'b', 'c']

3. Write a program to create a list with user input.

In [5]: L1=list(input("enter the list:"))


print(L1)

enter the list:12345


['1', '2', '3', '4', '5']

4.Write a program to find the second largest element in the list ‘Num’.

In [9]: num = [1,3,5,7,9]


num.sort()
print("The second largest element in the list is",num[3])

The second largest element in the list is 7

5.Write a program to count() the occurrence of an element in the list.

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))

enter the element to be checked:1


the number of occurences of: 1 is 2

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)

Enter element to be inserted:6


index of the inserted element:5
[1, 2, 3, 4, 5, 6]
7.Write a Python Program to find the smallest divisor of an integer.

In [29]: num = int(input("Enter the number whose divisor is to be calculated:"))


list1=[]
for i in range(2,num+1):
if num % i == 0:
list1.append(i)
break
list1.sort()
print("The smallest divisor of",num,"is",list1[0])

Enter the number whose divisor is to be calculated:1247


The smallest divisor of 1247 is 29

8.Write a Python Program to find the largest number in a list.

In [40]: L1 = [1,10,25,12,-10,37,49,71,109]
L1.sort()
print("The largest element is",L1[-1])

The largest element is 109

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)

The even elements in the list are [2, 16, 30]


The odd elements in the list are [5, 3, 47, 29]

10.Write a program to merge two lists and sort it.

In [43]: L1 = [1,7,11,2,9,3]
L2 = [27,31,19,16,12,6]
L1.extend(L2)
L1.sort()
print(L1)

[1, 2, 3, 6, 7, 9, 11, 12, 16, 19, 27, 31]

11.Write a program to add only multiples of 3 from the given list.

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)

The sum of the multiples of 3 is: 72

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)

The multiplied list is: [2, 6, 10, 14, 18]

13.Write a program to take list and display the average of numbers.

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))

The average is: 12.714285714285714

14.Write a menu driven program to perform various list operations:

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")

choice = int(input("Enter your choice: "))


if choice == 1:
element = input("Enter the element to append: ")
my_list.append(element)
elif choice == 2:
element = input("Enter the element to insert: ")
position = int(input("Enter the position to insert: "))
my_list.insert(position, element)
elif choice == 3:
sublist = input("Enter a list of elements to append (space-separated): ")
my_list.extend(sublist)
elif choice == 4:
position = int(input("Enter the position of the element to modify: "))
new_element = input("Enter the new element: ")
my_list[position]=new_element
elif choice == 5:
position = int(input("Enter the position of the element to delete:"))
del my_list[position]
elif choice == 6:
value = (input("Enter the value of the element to delete:"))
my_list.remove(value)
elif choice == 7:
print("Exiting...")
else:
print("Invalid choice!")

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']

15.Give the output of the following codes:-

In [56]: #(a)
list=['p','r','o','b','l','e',',m']
list[1:3]=[]
print(list)
list[2:5]=[]
print(list)

['p', 'b', 'l', 'e', ',m']


['p', 'b']

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)

enter size of list: 5


enter 0 element : 2
enter 1 element : 10
enter 2 element : 37
enter 3 element : 46
enter 4 element : 32
Numbers in the list are
[2, 10, 37, 46, 32]
Maximum value in the list = 46

In [ ]: !jupyter nbconvert --to html

You might also like