We can have a list or a dictionary as a value of an item in a dictionary, but we cannot use these data types as keys of the dictionary. If we try to, we will get a TypeError
. i really dont understand these lines
1 Like
dictionaries consist of key value pairs:
d = {
'key': 'value'
}
what these lines are saying is that the value of a dictionary can be a dictionary or a list:
d = {
'my_list': ['item1', 'item2', 'item3'],
'my_dict': {'key': 'value'}
}
the keys (my_list
, my_dict
and key
) are strings. So that is valid. The moment you attempt to use a list or dictionary as key for a dictionary, you will get a type error.
11 Likes
A post was split to a new topic: S there any specific reason why to use a list as a key
Keys must be unique all time and whatever happens. Lists and dictionaries can be changed.
1 Like