*Memo:
- My post explains a dictionary (2).
- My post explains a dictionary (3).
- My post explains a dictionary (4).
- My post explains a dictionary (5).
- My post explains a dictionary (6).
- My post explains a dictionary (7).
- My post explains a dictionary (8).
- My post explains dictionary functions (1).
- My post explains a dictionary(dict) comprehension.
- My post explains a dictionary shallow and deep copy.
- My post explains a list (1).
- My post explains a tuple (1).
- My post explains a set (1).
- My post explains a frozenset (1).
- My post explains an iterator (1).
- My post explains a string (1).
- My post explains a bytes (1).
- My post explains a bytearray (1).
- My post explains a range (1).
A dictionary(dict):
- is the ordered mutable(unhashable) collection of zero or more pairs of keys and values(
key:value) whose type isdict:-
Ordered means that the order of each pair of a key and value in a dictionary is kept so it guarantees that the order is always the same unless changed:
- From Python 3.7, dictionaries are ordered so until Python 3.6, dictionaries are unordered.
- Mutable(Unhashable) means the elements of a dictionary can be changed.
-
Ordered means that the order of each pair of a key and value in a dictionary is kept so it guarantees that the order is always the same unless changed:
- doesn't allow duplicated keys (even with different types):
- The last duplicated key and its value are used.
- allows duplicated values.
- can have the hashable types of keys and any types of values:
- A hashable type is the type whose value cannot be changed like
str,bytes,int,float,complex,bool,tuple,frozenset,rangeoriterator.
- A hashable type is the type whose value cannot be changed like
- cannot have the unhashable types of keys:
- A unhashable type is the type whose value can be changed like
bytearray,list,setordict.
- A unhashable type is the type whose value can be changed like
- can be used with len() to get the length.
- is
Trueif it's non-empty andFalseif it's empty, checking it with bool(). - is
Falseif it's non-empty andTrueif it's empty, inverting the truth value withnotkeyword. - can be checked if a specific key and/or value are and aren't in the dictionary with
inkeyword and withnotandinkeyword respectively. - can be checked if the dictionary is and isn't referred to by two variables with
iskeyword and withisandnotkeyword respectively:- Dictionary(Dict) literals with
iskeyword and withisandnotkeyword don't get warnings respectively.
- Dictionary(Dict) literals with
- and other dictionary can be checked if all the keys and/or values in them are and aren't the same with
==and!=respectively. - and other dictionary can be checked if only all the keys or both all the keys and values in:
- the dictionary are in other dictionary with
<=. - other dictionary are in the dictionary with
>=. - the dictionary and other elements are in other dictionary with
<. - other dictionary and other elements are in the dictionary with
>.
- the dictionary are in other dictionary with
- and other dictionary can be checked if they have and don't have only their common keys or both their common keys and values with
bool()and&and withnotkeyword and&respectively. - cannot be enlarged with
*and a number. - and other dictionaries cannot be concatenated with
+. - and other dictionary can return:
- only all the keys or both all the keys and values in them with
'|'(Union: A ∪ B). - only their common keys or both their common keys and values with
'&'(Intersection: A ∩ B). - only the keys or both the keys and values in the dictionary which aren't in other dictionary with
'-'(Difference: A - B). - only the keys or both the keys and values in either the dictionary or other dictionary but not both with
'^'(Symmetric Difference: A Δ B).
- only all the keys or both all the keys and values in them with
- can be iterated with a
forstatement. - can be unpacked with an assignment and
forstatement, function,*and**. - can be created by
{}with or without',', by dict() in many ways and by a dictionary(dict) comprehension:-
dict()can create a dictionary withdict, keyword arguments(kwargs) and a 2D iterable(list,tuple,set,frozensetoriterator). -
dict()can also create a dictionary with an empty 1D iterable. -
dict()cannot create a dictionary with a non-empty 1D iterable exceptdict. - For
dict(), the words type conversion are also suitable in addition to the word creation.
-
- cannot be big because it gets
MemoryError. - can be read by keying.
- can be changed by keying and a del statement.
- can be used like a list to be read or changed by indexing but not by slicing properly.
- can be continuously used through multiple variables.
- can be shallow-copied by dict.copy(), copy.copy() and
dict(). - can be deep-copied by copy.deepcopy().
Be careful, a big dictionary gets MemoryError.
MemoryError.{} with or without ',' can create a dictionary as shown below:
v = {} # Empty 1D dict
v = {'name':'John', 'age':36, 'gender':'Male'} # 1D dict
v = {'name':'John', 'age':36, 'name':'Anna', 'age':24} # 1D dict
v = {'name1':'John', 'age1':36, 'name2':'John', 'age2':36} # 1D dict
v = {0:'apple', 1:'orange', 2:'kiwi'} # 1D dict
v = {'person1':{'name':'John', 'age':36}, # 2D dict
'person2':{'name':'Anna', 'age':24}}
v = {'person1':{'name':{'fname':'John', 'lname':'Smith'}}, # 3D dict
'person2':{'name':{'fname':'Anna', 'lname':'Brown'}}}
# No error
print({0:'A', 0.0:'B', 0.0+0.0j:'C', False:'D'})
print({1:'A', 1.0:'B', 1.0+0.0j:'C', True:'D'})
print({'A':0, 'B':0.0, 'C':0.0+0.0j, 'D':False})
print({'A':1, 'B':1.0, 'C':1.0+0.0j, 'D':True})
print({'A':'a', b'A':b'a', 'BA':bytearray(b'a'),
2:3, 2.3:4.5, 2.3+4.5j:6.7+8.9j, True:False,
'L':[4, 5], (2, 3):(4, 5), 'S':{4, 5},
frozenset({2, 3}):frozenset({4, 5}), 'D':{'A':'a'},
range(2, 3):range(4, 5),
iter([2, 3]):iter([4, 5])})
print({'':'A', b'': b'A', ():(2, 3),
frozenset():frozenset({2, 3}), range(0):range(2, 3),
iter([2, 3]):iter([2, 3])})
print(len({'fname':'John', 'lname':'Smith', 'age':36, 'gender':'Male'}))
print(bool({0:0}))
print(bool({():{}}))
print(bool({}))
print(not {0:0})
print(not {():{}})
print(not {})
print(bool({0:0}.keys()))
print(bool({():{}}.keys()))
print(bool({}.keys()))
print(not {0:0}.keys())
print(not {():{}}.keys())
print(not {}.keys())
print(bool({0:0}.values()))
print(bool({():{}}.values()))
print(bool({}.values()))
print(not {0:0}.values())
print(not {():{}}.values())
print(not {}.values())
print(bool({0:0}.items()))
print(bool({():{}}.items()))
print(bool({}.items()))
print(not {0:0}.items())
print(not {():{}}.items())
print(not {}.items()) # Empty dict
print('A' in {'A':'B', 'C':{'D':'E'}})
print('A' in {'A':'B', 'C':{'D':'E'}}.keys())
print('A' not in {'A':'B', 'C':{'D':'E'}})
print('A' not in {'A':'B', 'C':{'D':'E'}}.keys())
print('B' in {'A':'B', 'C':{'D':'E'}}.values())
print('B' not in {'A':'B', 'C':{'D':'E'}}.values())
print(('A', 'B') in {'A':'B', 'C':{'D':'E'}}.items())
print(('A', 'B') not in {'A':'B', 'C':{'D':'E'}}.items())
print({0:1, 2:3} is {0:1, 2:3}) # No warning
print({0:1, 2:3} is not {0:1, 2:3}) # No warning
print({0:1, 2:3}.keys() is {0:1, 2:3}.keys()) # No warning
print({0:1, 2:3}.keys() is not {0:1, 2:3}.keys()) # No warning
print({0:1, 2:3}.values() is {0:1, 2:3}.values()) # No warning
print({0:1, 2:3}.values() is not {0:1, 2:3}.values()) # No warning
print({0:1, 2:3}.items() is {0:1, 2:3}.items()) # No warning
print({0:1, 2:3}.items() is not {0:1, 2:3}.items()) # No warning
print({0:1, 2:3, 4:5} == {0:1, 2:3, 4:5})
print({0:1, 2:3, 4:5} != {0:1, 2:3, 4:5})
print({0:1, 2:3, 4:5}.keys() == {0:1, 2:3, 4:5}.keys())
print({0:1, 2:3, 4:5}.keys() != {0:1, 2:3, 4:5}.keys())
print({0:1, 2:3, 4:5}.values() == {0:1, 2:3, 4:5}.values())
print({0:1, 2:3, 4:5}.values() != {0:1, 2:3, 4:5}.values())
print({0:1, 2:3, 4:5}.items() == {0:1, 2:3, 4:5}.items())
print({0:1, 2:3, 4:5}.items() != {0:1, 2:3, 4:5}.items())
print({0:1, 2:3, 4:5}.keys() <= {0:1, 2:3, 4:5}.keys())
print({0:1, 2:3, 4:5}.keys() >= {0:1, 2:3, 4:5}.keys())
print({0:1, 2:3, 4:5}.keys() < {0:1, 2:3, 4:5}.keys())
print({0:1, 2:3, 4:5}.keys() > {0:1, 2:3, 4:5}.keys())
print({0:1, 2:3, 4:5}.items() <= {0:1, 2:3, 4:5}.items())
print({0:1, 2:3, 4:5}.items() >= {0:1, 2:3, 4:5}.items())
print({0:1, 2:3, 4:5}.items() < {0:1, 2:3, 4:5}.items())
print({0:1, 2:3, 4:5}.items() > {0:1, 2:3, 4:5}.items())
print(bool({0:1, 2:3, 4:5}.keys() & {2:3, 6:7}.keys()))
print(not ({0:1, 2:3, 4:5}.keys() & {2:3, 6:7}.keys()))
print(bool({0:1, 2:3, 4:5}.items() & {2:3, 6:7}.items()))
print(not ({0:1, 2:3, 4:5}.items() & {2:3, 6:7}.items()))
print({0:1, 8:9} | {0:1, 4:5, 8:9})
print({0:1, 8:9}.keys() | {0:1, 4:5, 8:9}.keys())
print({0:1, 2:3, 4:5, 6:7}.keys() & {0:1, 4:5, 8:9}.keys())
print({0:1, 2:3, 4:5, 6:7}.keys() - {0:1, 4:5, 8:9}.keys())
print({0:1, 2:3, 4:5, 6:7}.keys() ^ {0:1, 4:5, 8:9}.keys())
print({0:1, 8:9}.items() | {0:1, 4:5, 8:9}.items())
print({0:1, 2:3, 4:5, 6:7}.items() & {0:1, 4:5, 8:9}.items())
print({0:1, 2:3, 4:5, 6:7}.items() - {0:1, 4:5, 8:9}.items())
print({0:1, 2:3, 4:5, 6:7}.items() ^ {0:1, 4:5, 8:9}.items())
for x in {0:1, 2:3, 4:5}: print(x)
for x in {0:{1:2}, 3:{4:5}, 6:{7:8}}: print(x)
v1, v2, v3 = {0:1, 2:3, 4:5}; print(v1, v2, v3)
v1, *v2, v3 = {0:1, 2:3, 4:5, 6:7, 8:9, 10:11}; print(v1, v2, v3)
for v1, v2, v3 in [{0:1, 2:3, 4:5}, {6:7, 8:9, 10:11}]: print(v1, v2, v3)
for v1, *v2, v3 in [{0:1, 2:3, 4:5, 6:7, 8:9, 10:11},
{12:13, 14:15, 16:17, 18:19, 20:21, 22:23}]:
print(v1, v2, v3)
print(*{0:1, 2:3, 4:5, 6:7}, *{8:9, 10:11})
print([*{0:1, 2:3, 4:5, 6:7}, *{8:9, 10:11}])
print({**{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}})
print({**{0:1, 2:3}, 4:5, **{6:7, 8:9, **{10:11}}})
print({x:x**2 for x in [0, 1, 2, 3, 4, 5, 6, 7]})
print({x: {y:y**2 for y in x} for x in ((0, 1, 2, 3), (4, 5, 6, 7))})
print({x: {y: {z:z**2 for z in y} for y in x} for x in (((0, 1), (2, 3)),
((4, 5), (6, 7)))})
# No error
print({[2, 3]:'val'})
print({{2, 3}:'val'})
print({{'A':'a'}:'val'})
print({bytearray(b'Hello'):'val'})
print({0:1, 2:3, 4:5} <= {0:1, 2:3, 4:5})
print({0:1, 2:3, 4:5} >= {0:1, 2:3, 4:5})
print({0:1, 2:3, 4:5} < {0:1, 2:3, 4:5})
print({0:1, 2:3, 4:5} > {0:1, 2:3, 4:5})
print({0:1, 2:3, 4:5} <= {0:1, 2:3, 4:5}.values())
print({0:1, 2:3, 4:5} >= {0:1, 2:3, 4:5}.values())
print({0:1, 2:3, 4:5} < {0:1, 2:3, 4:5}.values())
print({0:1, 2:3, 4:5} > {0:1, 2:3, 4:5}.values())
print(bool({0:1, 2:3, 4:5} & {2:3, 6:7}))
print(not ({0:1, 2:3, 4:5} & {2:3, 6:7}))
print(bool({0:1, 2:3, 4:5}.values() & {2:3, 6:7}.values()))
print(not ({0:1, 2:3, 4:5}.values() & {2:3, 6:7}.values()))
print({'name':'John', 'age':36} * 3)
print({'name':'John', 'age':36}.keys() * 3)
print({'name':'John', 'age':36}.values() * 3)
print({'name':'John', 'age':36}.items() * 3)
print({0:1, 2:3, 4:5} + {6:7, 8:9} + {10:11, 12:13, 14:15, 16:17})
print({0:1, 2:3, 4:5}.keys() + {6:7, 8:9}.keys() \
+ {10:11, 12:13, 14:15, 16:17}.keys())
print({0:1, 2:3, 4:5}.values() + {6:7, 8:9}.values() \
+ {10:11, 12:13, 14:15, 16:17}.values())
print({0:1, 2:3, 4:5}.items() + {6:7, 8:9}.items() \
+ {10:11, 12:13, 14:15, 16:17}.items())
print({0:1, 2:3, 4:5, 6:7} & {0:1, 4:5, 8:9})
print({0:1, 2:3, 4:5, 6:7} - {0:1, 4:5, 8:9})
print({0:1, 2:3, 4:5, 6:7} ^ {0:1, 4:5, 8:9})
print({0:1, 8:9}.values() | {0:1, 4:5, 8:9}.values())
print({0:1, 2:3, 4:5, 6:7}.values() & {0:1, 4:5, 8:9}.values())
print({0:1, 2:3, 4:5, 6:7}.values() - {0:1, 4:5, 8:9}.values())
print({0:1, 2:3, 4:5, 6:7}.values() ^ {0:1, 4:5, 8:9}.values())
print({x:x for x in range(1000000000)})
# Error
Top comments (0)