+ 7
What is a "key" in Python dictionaries?
I have been struggling to understand the definition of a dictionary. In the Sololearn lessons, they keep using the word "key" when defining what a dictionary is. But they never explained what a key is. I looked for help in the community but everyone else keeps using the word "key". đ” Please help. đ”đ”đ”
3 Answers
+ 12
Dictionaries store key-value pairs.
Keys are analogous to indexes of a list. When using lists you access the elements via the index. With dictionaries you access values via the keys.
The keys can be of any datatype (int, float, string, and even tuple).
A dictionary may contain duplicate values inside it, but the keys MUST be unique (so it isn't possible to access different values via the same key).
Example:
my_dict = {'blue': 9, 'dinosaur': 5, 'smoke': 11}
# the keys
print(my_dict.keys())
# the values
print(my_dict.values())
# how to access a value via key
print(my_dict['smoke'])
I hope this helps! đđđ»
+ 1
if we want to assign the value to any thing it helps to store the value in our understandable language
0
Each element in a dictionary is represented by a key:value pair.