dictionaries in python important questions

dictionaries in python important questions class 11

computer science with python

Q 1. How are dictionaries different from lists ?

Ans. The dictionary is similar to lists in the sense that it is also a collection of data-items just like lists BUT it is different from lists in the sense that lists are sequential collections (ordered) and dictionaries are non-sequential collections (unordered).
In lists, where there is an order associated with the data-items because they act as storage units for other objects or variables you’ve created. Dictionaries are different from lists and tuples because the group of objects they hold aren’t in any particular order, but rather each objects has its own unique name, commonly known as a key.

Q 2. How are objects stored in lists and dictionaries different ?

Ans. The objects or values stored in a dictionary can basically be anything (even the nothing type defined as None), but keys can only be immutable type-objects. e.g., strings, tuples, integers, etc. (dictionaries in python important questions)

Q 3. when are dictionaries more useful than lists ?

Ans. Dictionaries can be much more useful than lists. For example, suppose we wanted to store all our friends’ cell-phone numbers. We could create a list of pairs, (name of friend, phone number), but once this list becomes long enough searching this list for a specific phone number will get time-consuming. Better would be if we could index the list by our friends’s name. This is precisely what a dictionary does.

Q 4. Can sequence operations such as slicing and concatenation be applied to dictionaries ? why ?

Ans. No, sequence operations like slicing and concatenation cannot be applied on dictionaries. The reasons being, a dictionary is not a sequence. Because it is not maintained in any specific order, operations that depend on a specific order cannot be used.

 

Q 5. Why can’t Lists be used as keys ?

Ans. Lists cannot be used as keys in a dicitonary because they are mutable. And a Python dictionary can have only keys of immutable types.

 

Q 6. If the addition of a new key:value pair causes the size of the dictionary to grow beyond its original size, an error occurs. True or False ?

Ans. False. There cannot occur an error because Dictionaries being the mutable types, they can grow or shrink on and as-needed basis.

Q 7. Consider the following code fragments. What outputs will they produce ?

    (i) aDict = { 'Bhavna' : 1, "Richard" : 2, "Firoza" : 10, "Arshnoor" : 20}
        temp = 0
        for value in aDict.values( ) :
            temp = temp+value
        print(temp)
    (ii)      aDict = { 'Bhavna' : 1, "Richard" : 2, "Firoza" : 10, "Arshnoor" : 20}
              temp = " "
              for key in aDict.values( ) :
                    if temp < key :
                         temp = key
              print(temp)
    (iii)      aDict = { 'Bhavna' : 1, "Richard" : 2, "Firoza" : 10, "Arshnoor" : 20}
                k = "Bhavna"
                v = -1
                if k in aDict :
                      aDict[k] = v
                print(aDict)

ans.
(i) 33 (ii) Richard
(iii) { ‘Bhavna’ : -1, ‘Richard’ : 2, ‘Fizora’ : 10, ‘Arshnoor’ : 20 }

Dictionaries in python notes

Dictionary in python more details

 

You may also like...

1 Response

  1. November 8, 2020

    […] dictionary in python class 11 important questions […]

Leave a Reply

Your email address will not be published. Required fields are marked *