Name

Dictionary

Examples
# Create a new dictionary
element_names = {'H': 'hydrogen', 'He': 'helium', 'Li': 'lithium'}

# Add another key/value pair to the dictionary
element_names['Be'] = 'beryllium'
element_names['B'] = 'boron'
element_names['C'] = 'carbon'

# Loop over elements of dictionary. Note that key/value pairs
# are returned in an arbitrary order!
for k, v in element_names.iteritems():
	print("symbol: " + k + "; name: " + v)

# Get an individual value by key
print(element_names['He']) # Prints 'helium'

# Delete an item from a dictionary
del element_names['C']

# Keys don't have to be strings! They can be of any (hashable)
# type. Values can also be of any type.
number_map = {
  1: [0.5, 1.0, 2.0],
  2: [10.0, 15.0, 20.0],
  3: [50.0, 75.0, 100.0]
}
print(number_map[2]) # Prints [10.0, 15.0, 20.0]

Description
Syntax
Parameters
key
value
Methods
keys()
items()
iteritems()
values()
update()
Related {} (Curly braces)
[] (Index brackets)
Updated on Wed Jan 18 17:32:21 2017.
Creative Commons License