Creating 2D dictionary in Python
Question:
I have a list of details from an output for “set1” which are like “name”, “place”, “animal”, “thing”
and a “set2” with the same details.
I want to create a dictionary with dict_names[setx]['name']...
etc On these lines.
Is that the best way to do it? If not how do I do it?
I am not sure how 2D works in dictionary.. Any pointers?
Answers:
It would have the following syntax
dict_names = {
'd1': {
'name': 'bob',
'place': 'lawn',
'animal': 'man'
},
'd2': {
'name': 'spot',
'place': 'bed',
'animal': 'dog'
}
}
You can then look things up like
>>> dict_names['d1']['name']
'bob'
To assign a new inner dict
dict_names['d1'] = {'name': 'bob', 'place': 'lawn', 'animal': 'man'}
To assign a specific value to an inner dict
dict_names['d1']['name'] = 'fred'
Something like this would work:
set1 = {
'name': 'Michael',
'place': 'London',
...
}
# same for set2
d = dict()
d['set1'] = set1
d['set2'] = set2
Then you can do:
d['set1']['name']
etc. It is better to think about it as a nested structure (instead of a 2D matrix):
{
'set1': {
'name': 'Michael',
'place': 'London',
...
}
'set2': {
'name': 'Michael',
'place': 'London',
...
}
}
Take a look here for an easy way to visualize nested dictionaries.
Something like this should work.
dictionary = dict()
dictionary[1] = dict()
dictionary[1][1] = 3
print(dictionary[1][1])
You can extend it to higher dimensions as well.
If I understand your question properly, what you need here is a nested dictionary.You can use the addict module to create nested dictionaries quite easily.
https://github.com/mewwts/addict
if the items in each set is ordered, you could use the below approach
body = Dict()
setx = ['person1','berlin','cat']
sety = ['person2','jena','dog']
setz = ['person3','leipzig','tiger']
for set_,s in zip([setx,sety,setz],['x','y','z']):
for item,item_identifier in zip(set_,['name','city','animmal']):
body[f'set{s}'][f'{item_identifier}'] = item
now you can easily access the items as follows.
print(body['setx']['name'])
Simple one-liner for a nested 2D dictionary ( dict_names = {} ) :
dict_names.setdefault('KeyX',{})['KeyY']='Value'
I have a list of details from an output for “set1” which are like “name”, “place”, “animal”, “thing”
and a “set2” with the same details.
I want to create a dictionary with dict_names[setx]['name']...
etc On these lines.
Is that the best way to do it? If not how do I do it?
I am not sure how 2D works in dictionary.. Any pointers?
It would have the following syntax
dict_names = {
'd1': {
'name': 'bob',
'place': 'lawn',
'animal': 'man'
},
'd2': {
'name': 'spot',
'place': 'bed',
'animal': 'dog'
}
}
You can then look things up like
>>> dict_names['d1']['name']
'bob'
To assign a new inner dict
dict_names['d1'] = {'name': 'bob', 'place': 'lawn', 'animal': 'man'}
To assign a specific value to an inner dict
dict_names['d1']['name'] = 'fred'
Something like this would work:
set1 = {
'name': 'Michael',
'place': 'London',
...
}
# same for set2
d = dict()
d['set1'] = set1
d['set2'] = set2
Then you can do:
d['set1']['name']
etc. It is better to think about it as a nested structure (instead of a 2D matrix):
{
'set1': {
'name': 'Michael',
'place': 'London',
...
}
'set2': {
'name': 'Michael',
'place': 'London',
...
}
}
Take a look here for an easy way to visualize nested dictionaries.
Something like this should work.
dictionary = dict()
dictionary[1] = dict()
dictionary[1][1] = 3
print(dictionary[1][1])
You can extend it to higher dimensions as well.
If I understand your question properly, what you need here is a nested dictionary.You can use the addict module to create nested dictionaries quite easily.
https://github.com/mewwts/addict
if the items in each set is ordered, you could use the below approach
body = Dict()
setx = ['person1','berlin','cat']
sety = ['person2','jena','dog']
setz = ['person3','leipzig','tiger']
for set_,s in zip([setx,sety,setz],['x','y','z']):
for item,item_identifier in zip(set_,['name','city','animmal']):
body[f'set{s}'][f'{item_identifier}'] = item
now you can easily access the items as follows.
print(body['setx']['name'])
Simple one-liner for a nested 2D dictionary ( dict_names = {} ) :
dict_names.setdefault('KeyX',{})['KeyY']='Value'