*Memo:
- My post explains a dictionary (1).
- 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).
A dictionary can be read or changed by keying and a del statement as shown below:
*Memo:
- Keying can be done with one or more
[key]:-
key(Required-Type:Hashable):- Don't use
key=.
- Don't use
- Error occurs if
keydoesn't exist.
-
- A
delstatement can remove one or more pairs of a key and value from a dictionary by keying and can remove one or more variables themselves.
<1D dictionary>:
v = {'name':'John', 'age':36}
v = dict(name='John', age=36)
v = dict([('name', 'John'), ('age', 36)])
print(v['name'], v['age'])
# John 36
v['name'] = 'Emily'
v['gender'] = 'Female'
v['name'], v['gender'] = 'Emily', 'Female'
print(v)
# {'name': 'Emily', 'age': 36, 'gender': 'Female'}
del v['name'], v['gender']
print(v)
# {'age': 36}
del v
print(v)
# NameError: name 'v' is not defined
v = {'name':'John', 'age':36}
print(v['gender'])
del v['gender']
# KeyError: 'gender'
<2D dictionary>:
v = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}
v = dict(person1=dict(name='John', age=36),
person2=dict(name='Anna', age=24))
v = dict([('person1', dict([('name', 'John'), ('age', 36)])),
('person2', dict([('name', 'Anna'), ('age', 24)]))])
print(v['person1'], v['person2'])
# {'name': 'John', 'age': 36} {'name': 'Anna', 'age': 24}
print(v['person1']['name'], v['person1']['age'],
v['person2']['name'], v['person2']['age'])
# John 36 Anna 24
v['person1']['name'] = 'Emily'
v['person2']['gender'] = 'Female'
v['person3'] = {'name':'Peter', 'age':18, 'gender':'Male'}
print(v)
# {'person1': {'name': 'Emily', 'age': 36},
# 'person2': {'name': 'Anna', 'age': 24, 'gender': 'Female'},
# 'person3': {'name': 'Peter', 'age': 18, 'gender': 'Male'}}
del v['person1']
del v['person3']['name'], v['person3']['gender']
print(v)
# {'person2': {'name': 'Anna', 'age': 24, 'gender': 'Female'},
# 'person3': {'age': 18}}
<3D dictionary>:
v = {'person1':{'name':{'fname':'John', 'lname':'Smith'}},
'person2':{'name':{'fname':'Anna', 'lname':'Brown'}}}
v = dict(person1=dict(name=dict(fname='John', lname='Smith')),
person2=dict(name=dict(fname='Anna', lname='Brown')))
v = dict([('person1', dict([('name', dict([('fname', 'John'),
('lname', 'Smith')]))])),
('person2', dict([('name', dict([('fname', 'Anna'),
('lname', 'Brown')]))]))])
print(v['person1'], v['person2'])
# {'name': {'fname': 'John', 'lname': 'Smith'}}
# {'name': {'fname': 'Anna', 'lname': 'Brown'}}
print(v['person1']['name'], v['person2']['name'])
# {'fname': 'John', 'lname': 'Smith'}
# {'fname': 'Anna', 'lname': 'Brown'}
v['person1']['name']['fname'] = 'Emily'
v['person1']['name']['mname'] = 'Lee'
v['person2']['gender'] = 'Female'
v['person3'] = {'name':{'fname':'Peter', 'lname':'Davis'},
'gender':'Male'}
print(v)
# {'person1': {'name': {'fname': 'Emily', 'lname': 'Smith',
# 'mname': 'Lee'}},
# 'person2': {'name': {'fname': 'Anna', 'lname': 'Brown'},
# 'gender': 'Female'},
# 'person3': {'name': {'fname': 'Peter', 'lname': 'Davis'},
# 'gender': 'Male'}}
del v['person1']
del v['person2']['name']
del v['person3']['name']['lname'], v['person3']['gender']
print(v)
# {'person2': {'gender': 'Female'},
# 'person3': {'name': {'fname': 'Peter'}}}
A dictionary can be used like a list by indexing but not by slicing properly as shown below:
v = {0:'apple', 1:'orange', 2:'kiwi'}
print(v[0], v[1], v[2])
# Apple Orange Kiwi
v[0] = 'APPLE'
v[1] = 'ORANGE'
v[2] = 'KIWI'
print(v)
# {0: 'APPLE', 1: 'ORANGE', 2: 'KIWI'}
v[0:2] = ['banana', 'peach']
print(v)
# {0: 'APPLE', 1: 'ORANGE', 2: 'KIWI', slice(0, 2, None): ['banana', 'peach']}
A dictionary can be continuously used through multiple variables as shown below:
v1 = v2 = v3 = {'name':'John', 'age':36, 'gender':'Male'}
# Equivalent
# v1 = {'name':'John', 'age':36, 'gender':'Male'}
# v2 = v1
# v3 = v2
v1['name'] = 'Anna'
v2['age'] = 24
v3['gender'] = 'Female'
print(v1) # {'name': 'Anna', 'age': 24, 'gender': 'Female'}
print(v2) # {'name': 'Anna', 'age': 24, 'gender': 'Female'}
print(v3) # {'name': 'Anna', 'age': 24, 'gender': 'Female'}
A dictionary can be shallow-copied and deep-copied as shown below:
<Shallow copy>:
*Memo:
-
v1andv2refer to different outer dictionaries and the same inner dictionary. -
iscan check ifv1andv2refer to the same outer and inner dictionary. -
dict.copy(), copy.copy() and dict() shallow-copy a dictionary:
-
dict.copy()has no arguments.
-
import copy
v1 = {'a':'b', 'c':{'d':'e'}}
v2 = v1.copy()
v2 = copy.copy(v1)
v2 = dict(v1)
print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}
print(v1 is v2, v1['c'] is v2['c'])
# False True
v2['a'] = 'X'
v2['c']['d'] = 'Y'
# ↓↓↓ ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'Y'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
# ↑↑↑ ↑↑↑
<Deep copy>:
*Memo:
-
v1andv2refer to different outer and inner dictionaries.
*Memo:
- copy.deepcopy() deep-copies a dictionary.
-
copy.deepcopy()should be used because it's safe, deeply copying a dictionary whiledict.copy(),copy.copy()anddict()aren't safe, shallowly copying a dictionary.
import copy
v1 = {'a':'b', 'c':{'d':'e'}}
v2 = copy.deepcopy(v1)
print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}
print(v1 is v2, v1['c'] is v2['c'])
# False False
v2['a'] = 'X'
v2['c']['d'] = 'Y'
# ↓↓↓ ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
# ↑↑↑ ↑↑↑
Top comments (0)