Dictionaries in Python
Dictionaries in Python
Dictionaries in Python
1. How are dictionaries different from lists?
SOLUTION :-
The dictionary is similar to lists in the sense that it is also a collection of like lists BUT it is different from lists in the sense that lists are sequential collections (ordered) s dictionaries are non-sequential collections (unordered). In lists, where there is an order associated with the data-items because they act as storage unitat 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 object has its own uniq data-items name, commonly known as a key.
2. How are objects stored in lists and dictionaries different?
SOLUTION :-
The objects of values stored in a dictionary can basically be anything (even the nothing type defined as Nene), but keys can only be immutable type-objects r.g. strings, tuples, integrs, etc When are dictionaries more useful than lists? SOLUTION. Dictionaries can be much more useful than lists. For example, suppose we wanted to 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 the time-consuming, Better would be if we could index the list by our friend’s name.
3. what a dictionary does Can sequence operations such as slicing and concatenation be applied to dictionaries ? Why?
SOLUTION :-
No, sequence operations like slicing and concatenation cannot be applied on diction The reason 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.
4. Why can’t Lists be used as keys of dictionaries?
SOLUTION:-
Lists cannot be used as keys in a dictionary because they are mutable. And a Pyth dictionary can have only keys of immutable types. If the addition of a new keyalue pair causes the size of the dictionary to grow beyond its original size, a occurs.
5. Define Dictionary?
Solution :- Python Dictionary is used to store the data in a key-value pair format. The dictionary is the data type in Python, which can simulate the real-life data arrangement where some specific value exists for some particular key. It is the mutable data-structure. The dictionary is defined into element Keys and values.
Keys must be a single element
Value can be any type such as list, tuple, integer, etc.
In other words, we can say that a dictionary is the collection of key-value pairs where the value can be any Python object. In contrast, the keys are the immutable Python object, i.e., Numbers, string, or tuple.
6. How we can create a dictionary?
Solution :-
The dictionary can be created by using multiple key-value pairs enclosed with the curly brackets {}, and each key is separated from its value by the colon (:).The syntax to define the dictionary is given below.
Example :-
Dict = {“Name”: “Tom”, “Age”: 22}
Popular Videos
UX for Teams
Learn the basics and a bit beyond to improve your backend dev skills.
Designer
SEO & Instagram
Learn the basics and a bit beyond to improve your backend dev skills.
Designer