+ 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. đŸ˜”đŸ˜”đŸ˜”

19th Aug 2018, 3:36 PM
Christine
Christine - avatar
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! đŸ˜‰đŸ‘đŸ»
19th Aug 2018, 3:43 PM
Eduardo Petry
Eduardo Petry - avatar
+ 1
if we want to assign the value to any thing it helps to store the value in our understandable language
30th Aug 2018, 7:53 AM
Mohan Ayyappa
Mohan Ayyappa - avatar
0
Each element in a dictionary is represented by a key:value pair.
2nd Sep 2018, 5:50 AM
Shervin Woolcock