Python: Difference between revisions

269 bytes added ,  22 January 2020
No edit summary
Line 221: Line 221:


===Dictionaries===
===Dictionaries===
Dictionaries are hashmaps in Python
Dictionaries are hashmaps in Python<br>
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
# Create a dictionary
# Create a dictionary
Line 227: Line 227:
# Or
# Or
my_map = {1: "a", 2: "b"}
my_map = {1: "a", 2: "b"}
# Check if a key is in a dictionary
# O(1)
d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}
"1" in d
# Check if a value is in a dictionary
# Usually you should have a second dictionary if you need this functionality
# O(n)
'one' in d.values()
</syntaxhighlight>
</syntaxhighlight>