Chapter 4: Working with Lists and Dictionaries


NCERT solutions Working with Lists and Dictionaries

1. What will be the output of the following statements?
a) list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)

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


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

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


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

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


d) d) list1 = [1,2,3,4,5]
    list1[len(list1)-1]

Ans.: 5


2. Consider the following list myList. What will be the elements of myList after each of the following operations? 

myList = [10,20,30,40]



a) myList.append([50,60])

The output will be: [10,20,30,40,[50,60]]


b) myList.extend([80,90])

The output will be: [10,20,30,40,[50,60],80,90]


3. 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])

The output will be: 
1
3
5
7
9



NCERT solutions Working with Lists and Dictionaries


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

The output will be [1,2,3]


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

The output will be [6,7,8,9,10]


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

The output will be [2,4,6,8,10]


5. Differentiate between append() and extend() methods of list.

append()
i)Append add one element to a list at a time.
ii)The multiple values can be added using a list as a parameter but append adds them as a list only.

Examples
l1=[1,2,3]
l1.append([4,5])
print(l1)
The output will be: [1,2,3,[4,5]]

extend()
i).extend add multiple elements to a list at a time.
ii)If user adds multiple values in form of list as parameter, it will add separate values.

Examples
l1=[1,2,3]
l1.append([4,5])
print(l1)
The output will be: [1,2,3,4,5]



NCERT solutions Working with Lists and Dictionaries



6. Consider a list:
 list1 = [6,7,8,9]
What is the difference between the following  operations on list1:
a) lis t1 * 2
b) list1 *= 2
c) list1 = list1 * 2

a) The * operator treated as a replication operator when the list object is referred to with any numeric value. So here it will replicate the list values. Therefore it returns [6,7,8,9,6,7,8,9]

b) It is a shorthand operator and as explained earlier it will also produce a similar result like a question a).

c) It also produces a similar result as the statement is just similar.


7. The record of a student (Name, Roll No, Marks in  five subjects and percentage of marks) is stored in the following list: 
stRecord = [‘Raman’,’A-36′,[56,98,99,72,69], 78.8] 
Write Python statements to retrieve the following information from the list stRecord.
a) Percentage of the student

perc=stRecord[3]
print(perc)


b) Marks in the fifth subject

marks_sub5 = stRecord[2][4]
print(marks_sub5)


c) Maximum marks of the student

marks_max = max(stRecord[2])
print(marks_max)


d) Roll No. of the student

rno = stRecord[1]
print(rno)


e) Change the name of the student from ‘Raman’ to ‘Raghav’

sname = stRecord[0]
print(sname)


8. Consider the following dictionary stateCapital:


stateCapital = {“Assam”:”Guwahati”, “Bihar”:”Patna”,”Maharashtra”:”Mumbai”, “Rajasthan”:”Jaipur”} 
Find the output of the following statements:


a) print(stateCapital.get(“Bihar”))

The output will be – Patna


b) print(stateCapital.keys())

The output will be – dict_keys([‘Assam’, ‘Bihar’, ‘Maharashtra’, ‘Rajasthan’])


c) print(stateCapital.values())

The output will be – dict_values([‘Guwahati’, ‘Patna’, ‘Mumbai’, ‘Jaipur’])


d) print(stateCapital.items())

The output will be – dict_items([(‘Assam’, ‘Guwahati’), (‘Bihar’, ‘Patna’), (‘Maharashtra’, ‘Mumbai’), (‘Rajasthan’, ‘Jaipur’)])


e) print(len(stateCapital))

The output will be – 4


f) print(“Maharashtra” in stateCapital)

The output will be – True


g) print(stateCapital.get(“Assam”))

The output will be – Patna


h) del stateCapital[“Assam”]    print(stateCapital)

It will delete information related to Assam. and print other keys and values.
{‘Bihar’: ‘Patna’, ‘Maharashtra’: ‘Mumbai’, ‘Rajasthan’: ‘Jaipur’}




NCERT solutions Working with Lists and Dictionaries


Programming Problems



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



By using loops

l = [ ]
n = int(input(“Enter the no. of elements in the list:”))
cnt=0
for i in range(0,n):
    lval=int(input(“Enter the value to add in list:”))
    l.append(lval)
search_item=int(input(“Enter the value to search:”))
for i in range(0,len(l)):
    if l[i]==search_item:
        cnt+=1
print(search_item, “Found in the list”, cnt, ” times”)


With eval

lval = eval(input(“Enter the values for the list:”))
search_item=int(input(“Enter the value to search:”))
print(search_item, “Found in the list”, lval.count(search_item), ” times”)


2. 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 all three lists.

lval = eval(input(“Enter the values for the list:”))
pl =[ ]
nl =[ ]
for i in range(len(lval)):
    if lval[i]>0:
        pl.append(lval[i])
    elif lval[i]<0:
        nl.append(lval[i])
print(“Original List:”, lval)
print(“Poitive List:”, pl)
print(“Negative List:”, nl)


3. Write a program to find the largest and the second-largest elements in a given list of elements.

L = eval(input(“Enter the values for the list:”))
L.sort()
print(“largest element”,l[-1])
print(“second largest element”,l[-2])


4. 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.  

Hint

: Use an inbuilt function to sort the list.

import statistics as s
lval = eval(input(“Enter the values for the list:”))
m = s.median(lval)
print(“The median value is:”,m)
l = eval(input(“Enter the values:”))
l.sort( )
len1 =len(l)
mv = len1//2
if len1%2 ==0:
    m1, m2 = mv-1, mv
    md = (l[m1]+l[m2])/2
else:
    md=l[mv]
print(“The median value is:”,md)


5. 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.

l = eval(input(“Enter the values:”))
temp=[ ]
for i in l:
    if i not in temp:
        temp.append(i)
l=list(temp)
print(“List after removing duplicate elements:”,l)


6. 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.

l = [1,2,3]
list_value=int(input(“Enter the value to insert:”))
p=int(input(“Enter the position”))
idx = p – 1
l.insert(idx,list_value)
print(l)


7. Write a program to read elements of a list and do the following.
 
a) The program should ask for the position of the element to be deleted from the list and delete the element at the desired position in the list.

l = [1,2,3] 
p=int(input(“Enter the position to delete:”))
l.pop(p)
print(l)


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

l = [1,2,3]
value=int(input(“Enter the position to delete:”))
l.remove(value)
print(l)


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

d = {11:22,33:46,45:76}
s = sorted(d.values( ))
print(s[-1],s[-2])


9. Write a Python program to create a dictionary from a string ‘w3resource’ such that each individual character mates a key and its index value for fist occurrence males the corresponding value in the dictionary.


Expected output : {‘3’: 1, ‘s’: 4, ‘r’: 2, ‘u’: 6, ‘w’: 0, ‘c’: 8, ‘e’: 3, ‘o’: 5}

strr = ‘w3resource’
l = list(dict.fromkeys(strr))
l2 = list()
for i in l:
index = l.index(i)
l2.append((i,index))
d ={}
d.update(l2)
print(d)


10. Write a program to input your friend’s, names and their phone numbers and store them in the dictionary as the key-value pair. Perform the following operations on the dictionary:

n = int(input(‘Enter the number of friends ‘))
l = list()
print()
for i in range(n):
name = input(‘Enter the name ‘)
num = int(input(‘Enter the phone number ‘))
l.append((name , num))
print()
d={}


a) Display the Name and Phone number for all your friends

d = dict(l)
print(‘Name : Phone number’)
for i in d:
    print(i , ‘:’ , d[i])


b) Add a new key-value pair in this dictionary and display the modified dictionary

n = int(input(‘Enter how many more freinds you want to enter’))
for a in range(n):
    nn = input(‘Enter new name to add’)
    numm = int(input(‘Enter the phone number ‘))
l.append((nn , numm))
d= d.update(l)
print(d)
d = dict(l)
print(‘Name : Phone number’)
for i in d:
    print(i , ‘:’ , d[i])


c) Delete a particular friend from the dictionary

nn =input(‘Enter name to remove ‘)
del d[nn]
print(d)
print(nn,’has been removed’)


d) Modify the phone number of an existing friend

nam = input(‘Enter the name to change number ‘)
num = int(input(‘Enter phone number’))
d[nam] = num
print(‘Phone number of,’nam,’is modified’)


e) Check if a friend is present in the dictionary or not

nnm = input(‘Enter the name to search ‘)
if nnm in d:
        print(nnm ,’is present’)
else:
        print(nnm ,’not prsent’)


f) Display the dictionary in sorted order of names

print(dict(sorted(d.items())))



NCERT Class 11 Solutions can be of great value if you are trying to excel in your school examinations. The journey starts directly from when you step into class 1 as every class holds great to use as you progress. CBSE Class 11 is no doubt a very valuable class and the last class of your school life. Not to mention, it also leads to a lot of career-making decisions as you seem for some important competitive exams to get your desire college. NCERT solution can help you immensely throughout this adventure of yours.

NCERT Class 11 Solutions

Although higher classes students like Class 10-12 should have a better knowledge of all the topics that will help them to examine deeper into the basics as well as an advanced level of questions that are asked in many competitive exams after 12 class. One of the best ways to achieve it is by answering questions of NCERT Books with the help of NCERT solutions. We at SelfStudys strive hard to devise better ideas in order to help students and give you detailed solutions for all questions from the NCERT textbook as per the latest CBSE CCE marking scheme pattern is to back you up in useful learning in all the exams conducted by CBSE curriculum. The plethora of advantages of referring to NCERT solutions requires an in-depth understanding of detailed solutions of all NCERT book’s questions.


We have given all the detailed NCERT questions and solutions in PDF format which you can read for FREE. All you have to do is download the SelfStudys NCERT Solutions for Class 11 once for FREE and you are able to go. You can get all the Subject NCERT solutions by the links provided here. NCERT Solutions for all the subjects of Class 11 is available on this page including NCERT Class 11 Solution for Maths, Chemistry, Physics, English, Business Studies, Biology, economics are provided below to download in a free PDF file.



NCERT Solution for Class 11

NCERT Class 11 Solutions to the questions with Chapter-wise, detailed are given with the objective of helping students compare their answers with the example. NCERT books provide a strong foundation for every chapter. They ensure a smooth and easy knowledge of advanced concepts. As suggested by the HRD ministry, they will perform a major role in JEE.



NCERT Class 11 Solutions | NCERT Solution for Class 11

NCERT Solution for Class 11 includes all the questions given in NCERT Books for all Subjects. Here all questions are solved with detailed information and available for free to check. NCERT Solutions for Class 11 are given here for all chapter-wise. Select the subject and choose a chapter to view NCERT Solution chapter-wise. We hope that our NCERT Class 11 Solutions helped with your studies! If you liked our NCERT Solutions for Class 11, please share this post.



Why NCERT Solutions for Class 11 is Important?

NCERT Class 11 Solutions increases the base for conceptual knowledge. It provides a complete view of the prescribed syllabus, as the textbook doesn't have a detailed description of the syllabus nor solutions. All the important theorems and formulas of Class 11 are completely solved, which works to make the concepts and find new links to them. Importantly, the NCERT Class 11 Solutions is the point of reference for those students planning for competitive examinations such as NRRT, JEE Main/Advanced, BITSAT & VITEEE, etc. When the NCERT book is accompanied by the solutions, the knowledge of concepts becomes simple and in-depth. Moreover, students can trail into the solutions without break in topics, as it is designed to give a step-by-step explanation. Try out the Class 11 Solutions and learn from the resources.



Why try NCERT Class 11 Solutions?

Well, here are the solid reasons why you should and must try out the NCERT Class 11 Solutions.

Has ERRORLESS answers with 100% Reasoning

The quality of solutions is perfect

Gives in-depth knowledge with notes shortly after the solutions with shortcuts, tips, alternative methods & points to remember

Gives quick revision of the concepts included along with important definitions and Formulas on the chapters, which acts as a prepared refresher.

Designed step-by-step to give 100% Concept Clearness


No comments:

Post a Comment

Have query? Just say it.