Variables

Dictionaries in Python

Used to store key-value pairs.


Syntax
dictionary_name = {"key1": value1, "key2": value2} //initialize with pairs
dictionary_name["key3"] = value3 #add new pair
dictionary_name["key1"] #access value

Notes

Similar to a hash map in Java, or an object key-value store in Javascript.

Dictionaries come with helper functions which list keys and values, as well as check existence of keys in a dictionary (key in dictionary).


Example
days_of_week = {'Monday': 'mon', 'Tuesday': 'tues'}

days_of_week['Wednesday'] = 'wed' #add a key
print (days_of_week['Monday']) #will output 'mon'

< Tuples   |   Sets >

© 2018 SyntaxDB. All Rights Reserved.