Creating a nested dictionary with a for loop in Python - python

I am trying to get the Expected Output below where it makes subfolders named John1, John2, John3 below and then it adds {0: 'John', 1: 'John', 2: 'John', 3: 'John'} values to each essentially creating a nested dictionary. How would I be able to do that and get the Expected Output?
dicts = {}
keys = range(4)
name_vals = ['John'+str(k+1) for k in range(3)]
values = ["Hi", "I", "am", "John"]
for k in name_vals:
for i in keys:
for x in values:
dicts[k][i] = x
Expected Output:
{John1: {0: 'John', 1: 'John', 2: 'John', 3: 'John'},
John2: {0: 'John', 1: 'John', 2: 'John', 3: 'John'},
John3: {0: 'John', 1: 'John', 2: 'John', 3: 'John'}}

this is the way with list comprehension
values = ["Hi", "I", "am", "John"]
{ f'Jhon{k+1}': {i: values[3] for i in range(len(values))} for k in range(3)}
Output:
{'Jhon1': {0: 'John', 1: 'John', 2: 'John', 3: 'John'},
'Jhon2': {0: 'John', 1: 'John', 2: 'John', 3: 'John'},
'Jhon3': {0: 'John', 1: 'John', 2: 'John', 3: 'John'}}

you can try this
keys = range(4)
name_vals = ['John'+str(k+1) for k in range(3)]
values = ["Hi", "I", "am", "John"]
dicts = {}
for k in name_vals:
subdict = {}
for i in keys:
subdict[i] = values[i]
dicts[k] = subdict
dicts
output
{'John1': {0: 'Hi', 1: 'I', 2: 'am', 3: 'John'},
'John2': {0: 'Hi', 1: 'I', 2: 'am', 3: 'John'},
'John3': {0: 'Hi', 1: 'I', 2: 'am', 3: 'John'}}
if u want all values to be 'john' then just replace values[i] with values[3]

Related

Combinations of elements to form nested dictionary

I have a dictionary, with integers as key (1 up to n), and values being lists (1 up to m elements). I would like to create a new dictionary, with the primary key being an integer (1 up to each n * m), and the secondary key the original n key with values as strings from the list.
Input:
input = {
1: ['foo', ...],
2: ['buz', ...],
3: ['sam', 'foo', ...],
4: ['bar', ...],
5: ['foo', 'bar', ...]
...
}
Where ... marks more values being present. Values are sometimes the same, sometimes not, and order is not important here even though I used lists.
Output: for this particular case (if ... omitted).
output = {
1: {1: 'foo', 2: 'buz', 3: 'sam', 4: 'bar', 5: 'foo'},
2: {1: 'foo', 2: 'buz', 3: 'sam', 4: 'bar', 5: 'bar'},
3: {1: 'foo', 2: 'buz', 3: 'bar', 4: 'bar', 5: 'foo'},
4: {1: 'foo', 2: 'buz', 3: 'bar', 4: 'bar', 5: 'bar'}
}
Edit: second example
input = {
1: ['alice'],
2: ['tom', 'josh'],
3: ['mike', 'adam'],
4: ['kate', 'filip'],
}
output = {
1: {1: 'alice', 2: 'tom', 3: 'mike', 4: 'kate'},
2: {1: 'alice', 2: 'tom', 3: 'mike', 4: 'filip'},
3: {1: 'alice', 2: 'tom', 3: 'adam', 4: 'kate'},
4: {1: 'alice', 2: 'tom', 3: 'adam', 4: 'filip'},
5: {1: 'alice', 2: 'josh', 3: 'mike', 4: 'kate'},
6: {1: 'alice', 2: 'josh', 3: 'mike', 4: 'filip'},
7: {1: 'alice', 2: 'josh', 3: 'adam', 4: 'kate'},
8: {1: 'alice', 2: 'josh', 3: 'adam', 4: 'filip'},
}
If someone has at least a hint I would very much appreciate it. Since I can't find the working solution at all.
Thanks all in advance
You can use itertools.product and a nested dictionary comprehension:
from itertools import product
out = {i+1: {j+1: e for j,e in enumerate(x)}
for i,x in enumerate(product(*data.values()))}
output:
{1: {1: 'foo', 2: 'buz', 3: 'sam', 4: 'bar', 5: 'foo'},
2: {1: 'foo', 2: 'buz', 3: 'sam', 4: 'bar', 5: 'bar'},
3: {1: 'foo', 2: 'buz', 3: 'foo', 4: 'bar', 5: 'foo'},
4: {1: 'foo', 2: 'buz', 3: 'foo', 4: 'bar', 5: 'bar'}}
Input:
data = {1: ['foo'], 2: ['buz'], 3: ['sam', 'foo'], 4: ['bar'], 5: ['foo', 'bar']}

getting arrays of values from dictionary by key

I have dictionary:
teamDictionary = {
1: {'name': 'Bob', 'team': 'A', 'status': 'Leave'},
2: {'name': 'George', 'team': 'C', 'status': 'Training'},
3: {'name': 'Sam', 'team': 'B', 'status': 'Travel'},
4: {'name': 'Phil', 'team': 'A', 'status': 'Leave'},
5: {'name': 'Georgia', 'team': 'C', 'status': 'Training'}
}
I need to get array of names:
['Bob','George','Sam','Phil','Georgia']
How shold I solve my problem?
Using a list comprehension you can
Get the values in the dictionary.
For each of the values, get the name
TeamDictionary = {
1: {'name': 'Bob', 'team': 'A', 'status': 'Leave'},
2: {'name': 'George', 'team': 'C', 'status': 'Training'},
3: {'name': 'Sam', 'team': 'B', 'status': 'Travel'},
4: {'name': 'Phil', 'team': 'A', 'status': 'Leave'},
5: {'name': 'Georgia', 'team': 'C', 'status': 'Training'}
}
print([x['name'] for x in TeamDictionary.values()])
> ['Bob', 'George', 'Sam', 'Phil', 'Georgia']
This will work:
names = [value['name'] for key, value in teamDictionary.items()]
You can easyly do this in one line using array comprehension.
names = [item['name'] for item in teamDictionary.values()]
you can iterate through the dictionary keys and for each item you can 'take' the name property like:
names = [teamDictionary[key]['name'] for key in teamDictionary]

getting part of dictionary by value in python

I have dictionary:
teamDictionary = {
1: {'name': 'Bob', 'team': 'A', 'status': 'Leave'},
2: {'name': 'George', 'team': 'C', 'status': 'Training'},
3: {'name': 'Sam', 'team': 'B', 'status': 'Travel'},
4: {'name': 'Phil', 'team': 'A', 'status': 'Leave'},
5: {'name': 'Georgia', 'team': 'C', 'status': 'Training'}
}
I need to get all smaller dictionary where team is C. My cod is:
team_leave = [teamDictionary[a] for a, b in teamDictionary.items() if b['team'] == 'C' ]
print(team_leave)
[{'name': 'George', 'team': 'C', 'status': 'Training'}, {'name': 'Georgia', 'team': 'C', 'status': 'Training'}]
But I need to get
{
2: {'name': 'George', 'team': 'C', 'status': 'Training'},
5: {'name': 'Georgia', 'team': 'C', 'status': 'Training'}
}
How should I solve my problem?
You can use a dict comprehension instead:
{k: d for k, d in teamDictionary.items() if d['team'] == 'C'}
You should use Dictionary Comprehension:
team_leave = {key: item for key, item in teamDictionary.items() if item['team'] == 'C'}
print(team_leave)
Ouput:
{2: {'name': 'George', 'team': 'C', 'status': 'Training'}, 5: {'name': 'Georgia', 'team': 'C', 'status': 'Training'}}
print({key: values for key, values in teamDictionary.items() if values['team'] == 'C'}

How do I merge multiple dictionaries values having same key in python?

I have n number of dicts like this :
dict_1 = {1: {'Name': 'xyz', 'Title': 'Engineer'}, 2: {'Name': 'abc',
'Title': 'Software'}}
dict_2 = {1: {'Education': 'abc'}, 2: {'Education': 'xyz'}}
dict_3 = {1: {'Experience': 2}, 2:{'Experience': 3}}
.
.
.
dict_n
I just want to combine all of them based on main key like this :
final_dict = {1: {'Name': 'xyz', 'Title': 'Engineer', 'Education':
'abc', 'Experience': 2},
2: {'Name': 'abc', 'Title': 'Software', 'Education':
'xyz', 'Experience': 3}}
can anybody help me to achieve this ?
from your question I think you have n number of dicts. So make list of your dicts and combine all the values having same key. But that itself won't give the exact answer. They are list of dicts. So the second thing I have done is make all those small dicts to one dict .
Here my code you can check :
d1 = {1: {'Name': 'xyz', 'Title': 'Engineer'}, 2: {'Name': 'abc',
'Title': 'Software'}}
d2 = {1: {'Education': 'abc'}, 2: {'Education': 'xyz'}}
d3 = {1: {'Experience': 2}, 2:{'Experience': 3}}
ds = [d1, d2, d3] # list of your dicts you can change it to dict_
big_dict = {}
for k in ds[0]:
big_dict[k] = [d[k] for d in ds]
for k in big_dict.keys():
result = {}
for d in big_dict[k]:
result.update(d)
big_dict[k] = result
print(big_dict)
It gives O/P like this :
{
1: {'Education': 'abc', 'Title': 'Engineer', 'Name': 'xyz',
'Experience': 2},
2: {'Education': 'xyz', 'Title': 'Software', 'Name': 'abc',
'Experience': 3}
}

Working with Python nested dictionaries

I've got a dictionary (teamDictionary) that is seeded with names, teams, and statuses of team members:
teamDictionary = {
1: {'name': 'Bob', 'team': 'A', 'status': 'Leave'},
2: {'name': 'George', 'team': 'C', 'status': 'Training'},
3: {'name': 'Sam', 'team': 'B', 'status': 'Travel'},
4: {'name': 'Phil', 'team': 'A', 'status': 'Leave'},
5: {'name': 'Georgia', 'team': 'C', 'status': 'Training'}
}
How can I query the dictionary of dictionaries so that I can get the names of:
All team members from Team A that are out on Leave, or
All team members from Team B that are in a Travel status, or
All team members from Team C that are in Training
Thanks in advance!
I think list comprehensions with the conditions you want would look clean:
team_A_on_leave = [player['name'] for player in teamDictionary.values()
if player['team'] == 'A'
and player['status'] == 'leave']
The other 2 scenarios would be similar list comprehensions with different conditions.
We can filter the dictionary:
keys = filter(lambda x: teamDictionary.get(x).get('team') == 'A' and teamDictionary.get(x).get('status') == 'Leave', teamDictionary)
filtered_a = {k: teamDictionary.get(k) for k in keys}
{1: {'name': 'Bob', 'status': 'Leave', 'team': 'A'},
4: {'name': 'Phil', 'status': 'Leave', 'team': 'A'}}
You would just change the conditions based on the values you want to check for in the inner dictionaries.
You can try this:
teamDictionary = {
1: {'name': 'Bob', 'team': 'A', 'status': 'Leave'},
2: {'name': 'George', 'team': 'C', 'status': 'Training'},
3: {'name': 'Sam', 'team': 'B', 'status': 'Travel'},
4: {'name': 'Phil', 'team': 'A', 'status': 'Leave'},
5: {'name': 'Georgia', 'team': 'C', 'status': 'Training'}
}
a_leave = [b['name'] for a, b in teamDictionary.items() if b['team'] == 'A' and b['status'] == 'Leave']
b_travel = [b['name'] for a, b in teamDictionary.items() if b['team'] == 'B' and b['status'] == 'Travel']
c_training = [b['name'] for a, b in teamDictionary.items() if b['team'] == 'C' and b['status'] == "Training']

Categories

Resources