# Chapter 4 Working with list and Dictionary in Python Class 11 IP NCERT Solution

Share with others

## Q1. What will be the output of the following statements?

```i)

list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)

Ans. [10, 12, 26, 32, 65, 80]

ii)

list1 = [12,32,65,26,80,10]
sorted(list1)
print(list1)

Ans. [12, 32, 65, 26, 80, 10]

iii)

list1 = [1,2,3,4,5,6,7,8,9,10]
list1[: : -2]
print(list1[ : 3] + list1[ 3 : ])

Ans. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

iv)

list1 = [1,2,3,4,5]
print( list1 [ len ( list1 ) - 1 ] )
Ans. 5

#In Book no print statement is given for part (iii) and (iv) so actually NO OUTPUT, I am using the print statement in last line of both the parts and writing output accordingly```

## Q3. What will be the output of the following code segment:

```myList = [1,2,3,4,5,6,7,8,9,10]
for i in range(0, len(myList)):
if i % 2 == 0:
print(myList[i])```
```Show Answer
Ans.

1
3
5
7
9```

## Q4. What will be the output of the following code segment:

```a)

myList = [1,2,3,4,5,6,7,8,9,10]
del myList[3:]
print(myList)

Ans. [1, 2, 3]

b)

myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList)

Ans. [6, 7, 8, 9, 10]

c)

myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)

Ans. [2, 4, 6, 8, 10]```

## a) print(stateCapital.get(“Bihar”))b) print(stateCapital.keys())c) print(stateCapital.values())d) print(stateCapital.items())e) print(len(stateCapital))f) print(“Maharashtra” in stateCapital)g) print(stateCapital.get(“Assam”))h) del stateCapital[“Assam”] print(stateCapital)

```Show Answer
Ans.

a) Patna

b) dict_keys(['Assam', 'Bihar', 'Maharashtra', 'Rajasthan'])

c) dict_values(['Guwahati', 'Patna', 'Mumbai', 'Jaipur'])

d) dict_items([('Assam', 'Guwahati'), ('Bihar', 'Patna'), ('Maharashtra', 'Mumbai'), ('Rajasthan', 'Jaipur')])

e) 4

f) True

g) Guwahati

h) {'Bihar': 'Patna', 'Maharashtra': 'Mumbai', 'Rajasthan': 'Jaipur'}```

## Q1. Write a program to find the number of times an element occurs in the list.

```Show Answer
Ans.

L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 5, 2, 4]
e = int(input("Enter element to count  : "))
print("Element ", e, " occurs", L.count(e)," times in List")

OUTPUT :

Enter element to count  : 2
Element  2  occurs 3  times in List```

## Q2. Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print allthree lists.

```Show Answer
Ans.

n = int(input("How many elements you want to enter in a list?"))
ol = [ ]
pl = [ ]
nl = [ ]
for i in range(n):
e = int(input("Enter the element"))
ol.append(e)
for i in range(len(ol)):
if ol[i] >= 0:
pl.append(ol[i])
else:
nl.append(ol[i])
print("Original List is  : " , ol)
print("Positive List is  : " , pl)
print("Negative List is  : " , nl)

OUTPUT:

How many elements you want to enter in a list?5
Enter the element   : 1
Enter the element   : 2
Enter the element   : -3
Enter the element   : -4
Enter the element   : 5
Original List is  :  [1, 2, -3, -4, 5]
Positive List is  :  [1, 2, 5]
Negative List is  :  [-3, -4]```

## Q3. Write a program to find the largest and the second largest elements in a given list of elements.

```Show Answer
Ans.

L = [1, 23,  43, 21, 3, 76, 54, 43, 45]
L.sort()
print("Largest Element is : ", L[-1])
print("Second Largest Element is : ", L[-2])

OUTPUT:

Largest Element is :  76
Second Largest Element is :  54```

## Q4. Write a program to read a list of n integers and find their median.Note: The median value of a list of values is the middle one when they are arranged in order. If there are two middle values then take their average.

```Show Answer
Ans.

L = [1,23, 47, 21, 3, 76, 54, 43, 45, 100]
L.sort()
len = len(L)
if len%2 == 0:
med=(L[len//2] + L[len//2-1])/2
else :
med = L[len//2]
print("Median of given list is  : ", med)

OUTPUT :
Median of given list is : 44.0

```

## Q5. Write a program to read a list of elements. Modify this list so that it does not contain any duplicate elements, i.e., all elements occurring multiple times in the list should appear only once.

```Show Answer
Ans.

n = int(input("How many elements you want to enter in a list?"))
ol=[ ]
for i in range(n):
e = int(input("Enter the element : "))
ol.append(e)
len = len(ol)
ol.sort()
c=0
print("Oriiinal List is  : ", ol)
while(c<len-1):
if ol[c] == ol[c+1]:
ol.pop(c)
len=len-1
else:
c=c+1
print("List after removing duplicate Values is  : ", ol)

OUTPUT :

How many elements you want to enter in a list?7
Enter the element : 1
Enter the element : 2
Enter the element : 3
Enter the element : 2
Enter the element : 3
Enter the element : 4
Enter the element : 4
Original List is  :  [1, 2, 2, 3, 3, 4, 4]
List after removing duplicate Values is  :  [1, 2, 3, 4]```

## Q6. Write a program to create a list of elements. Input an element from the user that has to be inserted in the list. Also input the position at which it is to be inserted.

```Show Answer
Ans.

L = [1, 23,  43, 21, 3, 76, 54, 43, 45]
elem = int(input("Enter the number/element to be inserted"))
pos = int(input("Enter te posiion where the element to be inserted"))
L.insert(pos-1, elem)
print(L)

OUTPUT :

Enter the number/element to be inserted100
Enter te posiion where the element to be inserted1
[100, 1, 23, 43, 21, 3, 76, 54, 43, 45]```

## b) The program should ask for the value of the element to be deleted from the list and delete this value from the list.

```Show Answer
Ans. a)

L = [1, 23,  43, 21, 3, 76, 54, 43, 45]
pos = int(input("Enter te posiion of the element to be deleted"))
print("Original List : " , L)
L.pop(pos-1)
print("List after deleting element : " ,L)

OUTPUT :

Enter te posiion of the element to be deleted3
Original List :  [1, 23, 43, 21, 3, 76, 54, 43, 45]
List after deleting element :  [1, 23, 21, 3, 76, 54, 43, 45]

Ans. b)

L = [1, 23,  43, 21, 3, 76, 54, 43, 45]
elem = int(input("Enter the element to be deleted"))
print("Original List : " , L)
L.remove(elem)
print("List after deleting element : " ,L)

OUTPUT :

Enter the element to be deleted76
Original List :  [1, 23, 43, 21, 3, 76, 54, 43, 45]
List after deleting element :  [1, 23, 43, 21, 3, 54, 43, 45]```

## Q8. Write a Python program to find the highest 2 values in a dictionary.

```Show AnswerAns.

D = {'A' : 23, 'B' : 56, 'C' : 29, 'D' : 42, 'E' : 78 }
val = [ ]
v = D.values()
for i in v:
val.append(i)
val.sort()
print("Two maximum values are")
print(val[-1])
print(val[-2])

OUTPUT:

Two maximum values are
78
56
```

## Q9. Write a Python program to create a dictionary from a string â€˜w3resourceâ€™ such that each individual character makes a key and its index value for fist occurrence makes the corresponding value in dictionary.Expected output : {‘3’: 1, ‘s’: 4, ‘r’: 2, ‘u’: 6, ‘w’: 0, ‘c’: 8, ‘e’: 3, ‘o’: 5}

```Show Answer
Ans.

str1 = "w3resource"
dict = dict()
for i in str1:
dict[i] = str1.index(i)
print("The required dictionary: ")
print()
print(dict)```

## Perform the following operations on the dictionary:a) Display the name and phone number of all your friendsb) Add a new key-value pair in this dictionary and display the modified dictionaryc) Delete a particular friend from the dictionaryd) Modify the phone number of an existing friende) Check if a friend is present in the dictionary or notf) Display the dictionary in sorted order of names

```Show AnswerAns.

friends = { }
while True:
print("2. Display all ")
print("3. Delete any contact")
print("4. Modify/Change Phone Number")
print("5. Check if a friend is present or not")
print("6. Display in sorted order of names")
print("7. Exit")
ch = int(input("Enter your choice  : "))
if ch == 1 :
nm = input("Enter name  : ")
ph = input("Enter phone number  : ")
friends[ph] = nm
elif ch == 2 :
for i in friends :
print("Name  : " , friends[i])
print("Contact number : ", i)
elif ch == 3 :
c = input("Enter the phone number to delete")
if c in friends:
del friends[c]
else:
print("No Record found")
elif ch == 4:
p = input("Enter the contact number to be modified")
if p in friends:
n = input("Enter the new name")
friends[p] = n
print("Record Modified")
else:
print("No Record Found")
elif ch == 5:
ph = input("Enter the contact number to check")
if ph in friends:
print("Record Present")
else :
elif ch == 6:
for i in sorted(friends.values()):
print(i)
elif ch == 7:
break
else:
print("Enter Value between 0 and 7")```

## Working with list and Dictionary in Python

Disclaimer : I tried to give you the correct answers of “Working with List and Dictionary” , but if you feel that there is/are mistakes in the answers of Working with List and Dictionary given above, you can directly contact me at csiplearninghub@gmail.com.

## Working with list and Dictionary in Python

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series

Share with others
error: Content is protected !!