LANGUAGE » PYTHON

Dictionary

Constructors

python
my_dict = {'name': 'John', 'age': 28}
my_dict = dict(name='John', age=28)
my_dict = {expression for value in input_set (if predicate)}

Operators

Set value of key:

python
my_dict['hobby'] = 'games'

Delete key from dictionary:

python
del my_dict['key']

Methods

MethodDescription
getGet the value for key if key is in the dictionary, else default (None).
popSame as get, but removes the key.
keysList of keys.
valuesList of values.
updateMerge dicts.
copyCreates a shallow copy.
python
value = mydict.get(key)
value = my_dict.pop(key, None)

my_dict.update(another_dict)

Flow control

Check if key exists in mydict:

python
if key in mydict:
    print(mydict[key])

if key in mydict.keys():
    print(mydict[key])

Loop all keys and/or values:

python
for key in my_dict:
    print(key, my_dict[key])

for key, value in my_dict.items():
    print(key, value)

Examples

Get first key

Better performance than creating a list ang getting only the first element.

python
first_key = next(iter(my_dict))

Clear empty values

python
cleared = {key: value for key, value in errors.items() if value}

Copy

Shallow

python
copy1 = dict(my_dict)
copy2 = my_dict.copy()

Deep

python
import copy
deep_copy = copy.deepcopy(my_dict)