container : lists, strings, dictionaries, sets, tuplets
dic = {key1: value1, key2: value2, ...}
# iterable by key
dic.items() & dic.keys() & dic.values()
# ordered by key or value
sorted(dic) # by key
sorted(dic, reverse=True)
sorted(dic, key=dic.get) # by value
min(dic, key=dic.get) & max(dic, key=dic.get)
# return the key with maximun or minimun value
# get value
dic[key] & dic.get(key)
# if the key exist, dic[key] return error
# modify value / add new key-value pair / remove key
dic[key] = value
del dic[key]
dict.pop(key)
dic.updata({key: value})
#duplicate a dictionary (same to lists)
dic2 = dic1.copy()
# transform list of list to dictionary
dic1 = dir([['a', 1], ['b', 2]])
# list of dictionary & iteration
>>> animaux = [ani1, ani2]
>>> animaux
[{'n': 'g', 'p': 1, 't': 5}, {'n': 's', 'p': 7, 't': 1}]
>>> for ani in animaux:
... print(ani["n"])
...
girafe
singe
>>>len(animaux)
2 # length of dictionaries in a list
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.