Computer Science
How are individual elements of dictionaries accessed?
Python Dictionaries
CBSE
2 Likes
Answer
Individual elements of a dictionary can be accessed by using their corresponding keys as per the syntax shown below:
<dictionary-name> [<key>]
For example:
d = {'a': 1, 'b': 2, 'c': 3}
print(d['a'])
Output
1
In addition to this, we can also use the get( ) method to get value of the given key as per the syntax shown below:
<dictionary-name>.get(<key>, [default])
For example:
d = {'a': 1, 'b': 2, 'c': 3}
print(d.get('a'))
Output
1
Answered By
1 Like