Creating a list of dictionaries from a list of list - python

I'm currently stuck facing some issue with creating a list of dictionaries from a list of lists.
I'm confused with how to append each dictionary to empty list(not in my code). I've known there should be some code after loop processing, but I wasn't able to find out what it is.
l_members = [['Jhon', '19', 'male'],
['Takeru', '20', 'male'],
['Yuri', '22', 'female']
]
d_members = {}
for i in range(len(l_members)):
d_members['name'] = l_members[i][0]
d_members['age'] = l_members[i][1]
d_members['sex'] = l_members[i][2]
l_members.append(d_members)
print(d_members)
Here's a result of print(d_members)
{'name': 'Yuri', 'age': '22', 'sex': 'female'}
my expectation of the result is like below
[{'name': 'Jhon', 'age': '19', 'sex': 'male'},
{'name': 'Takeru', 'age': '20', 'sex': 'male'},
{'name': 'Yuri', 'age': '22', 'sex': 'female'}]
I would appreciate your help, thanks:)

Maybe a list comprehension might work:
d_members = [
{ 'name': name, 'age': age, 'sex': sex }
for name, age, sex in l_members
]

print the list, not the dict you created print(l_members)
create d_members in the loop, to avoid using the same again and again
then reassign to the i box, don't append because it'll only add it
for i in range(len(l_members)):
d_members = {}
d_members['name'] = l_members[i][0]
d_members['age'] = l_members[i][1]
d_members['sex'] = l_members[i][2]
l_members[i] = d_members
print(l_members)
Here are some shorten ways with enumerate, I put some to show you the possiblities
for i in range(len(l_members)):
l_members[i] = {'name': l_members[i][0], 'age': l_members[i][1], 'sex': l_members[i][2]}
for i, member in enumerate(l_members):
l_members[i] = {'name': member[0], 'age': member[1], 'sex': member[2]}
for i, (name, age, sex) in enumerate(l_members):
l_members[i] = {'name': name, 'age': age, 'sex': sex}
You can also use a list-comprehension to avoid using the indices and rebuilt directly the list, along with dict+zip to create the dict :
l_members = [dict(zip(['name', 'age', 'sex'], member)) for member in l_members]
# another one
l_members = [{'name': name, 'age': age, 'sex': sex} for (name, age, sex) in l_members]

As I've mentioned in the comments, this is because your last line of code which prints the value actually prints the d_members variable which will be the last value of the for-loop. You should instead be printing out the l_members variable which you appended the values to in your for-loop.
Additionally, you'll robably find out that the previous records still exist when you print l_members, so you'll have to either:
Remove the initial entries once you're done with the for loop (using list slicing):
l_members = l_members[3:]
Override the value for each iteration of the for loop:
l_members[i] = d_members
Or store the results in a new variable:
ld_members = []
# in your for loop:
ld_members.append(d_members)

Just append the dict to a new list inside the loop as below
list_of_dicts = []
for i in range(len(l_members)):
d_members = {}
d_members['name'] = l_members[i][0]
d_members['age'] = l_members[i][1]
d_members['sex'] = l_members[i][2]
list_of_dicts.append(d_members)
print(list_of_dicts)

Related

How to replace values in nested dictionaries by new values in a list in python

I am iterating through a list of dictionaries. I need to update the values for one specific key in all the dictionaries and I have the new values stored in a list. The list of new values is ordered so that the 1st new value belongs to a key in the 1st dictionary, 2nd new value to a key in the 2nd dictionary, etc.
My data looks something like this:
dict_list = [{'person':'Tom', 'job':'student'},
{'person':'John', 'job':'teacher'},
{'person':'Mary', 'job':'manager'}]
new_jobs = ['lecturer', 'cook', 'driver']
And I want to transform the list of dictionaries using the list of new jobs according to my description into this:
dict_list = [{'person':'Tom', 'job':'lecturer'},
{'person':'John', 'job':'cook'},
{'person':'Mary', 'job':'driver'}]
As I have a very long list of dictionaries I would like to define a function that would do this automatically but I am struggling how to do it with for loops and zip(), any suggestions?
I tried the for loop below. I guess the following code could work if it was possible to index the dictionaries like this dictionary['job'][i] Unfortunately dictionaries don't work like this as far as I know.
def update_dic_list():
for dictionary in dict_list:
for i in range(len(new_jobs)):
dictionary['job'] = new_jobs[i]
print(dict_list)
The output the code above gave me was this:
[{'person': 'Tom', 'job': 'driver'}, {'person': 'John', 'job': 'teacher'}, {'person': 'Mary', 'job': 'manager'}]
[{'person': 'Tom', 'job': 'driver'}, {'person': 'John', 'job': 'driver'}, {'person': 'Mary', 'job': 'manager'}]
[{'person': 'Tom', 'job': 'driver'}, {'person': 'John', 'job': 'driver'}, {'person': 'Mary', 'job': 'driver'}]
If your new_jobs list has the right job for each corresponding entry in the dict list, you could use zip:
dict_list = [{'person':'Tom', 'job':'student'},
{'person':'John', 'job':'teacher'},
{'person':'Mary', 'job':'manager'}]
new_jobs = ['lecturer', 'cook', 'driver']
for d, j in zip(dict_list, new_jobs):
d['job'] = j
print(dict_list)
prints
[{'person': 'Tom', 'job': 'lecturer'}, {'person': 'John', 'job': 'cook'}, {'person': 'Mary', 'job': 'driver'}]
With your loop, for each dictionary, you're going through the new jobs and updating that same dictionary over and over with each of the jobs until the last one. So by the end of it, they'll all be drivers. Because that's the last one.
You can do
dict_list = [{'person':'Tom', 'job':'student'},
{'person':'John', 'job':'teacher'},
{'person':'Mary', 'job':'manager'}]
new_jobs = ['lecturer', 'cook', 'driver']
def update_dic_list():
for job, _dict in zip(new_jobs, dict_list):
_dict['job'] = job
or
def update_dict_list():
for i, job in enumerate(new_jobs):
dict_list[i]['job'] = job
You only need to remove the inner loop because you are changing dictionary key job more than one time for each of item of outer loop:
def update_dic_list():
i = 0
for dictionary in dict_list:
dictionary['job'] = new_jobs[i]
i += 1
print(dict_list)
Or alternatively you could use enumerate:
def update_dic_list():
for i, dictionary in enumerate(dict_list):
dictionary['job'] = new_jobs[i]
print(dict_list)
Output:
[{'person': 'Tom', 'job': 'lecturer'}, {'person': 'John', 'job': 'cook'}, {'person': 'Mary', 'job': 'driver'}]

Check that exists only one entity in the values of the nested dictionary or not

people = {1: {'Name': 'John', 'Age': '27', 'Sex': 'Male'},
2: {'Name': 'Marie', 'Age': '22', 'Sex': 'Female'}}
I have this nested dictionary. I want to print True if all the values of Age will have a similar value. If any one of the values of Age is different, it will print False. Kindly explain how to do it?
As stated in the comment, you could do:
all_ages_equal = len({d['Age'] for d in people.values()})==1
You can save the first age and then check other Age is equal to the first or not.
(The first approach with any, when the generator face to an inequality break, so I think the first approach is faster than creating a set of all elements and then checking the length of set)
people = {1: {'Name': 'John', 'Age': '27', 'Sex': 'Male'},
2: {'Name': 'Marie', 'Age': '22', 'Sex': 'Female'}}
# first approach
first_age = list(people.values())[0]['Age']
if any(dct['Age'] != first_age for dct in people.values()):
print('different')
else:
print('equal')
# second approach
# Or check with `set` and check length of `set`
if len(set(dct['Age'] for dct in people.values())) > 1:
print('different')
else:
print('equal')
Output:
different
Grab any dictionary item (here, the first value). Compare all ages to the age of that item using all.
people = {1: {'Name': 'John', 'Age': '27', 'Sex': 'Male'},
2: {'Name': 'Marie', 'Age': '22', 'Sex': 'Female'}}
v0 = list(people.values())[0]
print(all([v['Age'] == v0['Age'] for v in people.values()]))
Another version, that isn't creating set or list first. It will short-circuit on first difference:
people = {
1: {"Name": "John", "Age": "27", "Sex": "Male"},
2: {"Name": "Marie", "Age": "22", "Sex": "Female"},
}
v = iter(people.values())
print(all(a["Age"] == b["Age"] for a, b in zip(v, v)))
Prints:
False

Sort list of dictionaries according to custom ordering

I need to sort list of dictionaries according to predefined list of values
I am using this part of code, and it will work fine but l_of_dicts has values which are not in a sort_l(predefined list of values)
l_of_dicts = [{'name':'Max','years':18},{'name':'John','years':25},{'name':'Ana','years':19},{'name':'Melis','years':38},{'name':'Ivana','years':38}]
sort_l = ['Ana','Melis','John','Max','Peter']
res = sorted(l_of_dicts , key = lambda ele: sort_l .index(list(ele.values())[0]))
I get an error :
ValueError: 'Ivana' is not in list
Is it posible to ignore that values? or even better extract them to another list?
The script bellow does the job but looks heavier than it should be. Maybe there's easier way to do it.
Script:
def sort_dict_list(dict_list, sorting_list):
result = []
not_used_values = []
temp = {}
for index, dictionary in enumerate(dict_list):
temp[dictionary['name']] = index
for item in sorting_list:
if item in temp.keys():
result.append(dict_list[temp[item]])
else:
not_used_values.append(item)
return result, not_used_values
You could do some preprocessing to store the ordering of only names in l_of_dicts:
l_of_dicts = [{'name': 'Max', 'years': 18}, {'name': 'John', 'years': 25},
{'name': 'Ana', 'years': 19}, {'name': 'Melis', 'years': 38}]
names_in_dicts = {d['name'] for d in l_of_dicts if 'name' in d}
sort_l = ['Ana', 'Melis', 'Ivana', 'John', 'Max', 'Peter']
sort_order = {name: order for order, name in enumerate(sort_l) if name in
names_in_dicts}
print(sort_order) # {'Ana': 0, 'Melis': 1, 'John': 3, 'Max': 4}
sorted_l_of_dicts = sorted(l_of_dicts, key=lambda d: sort_order[d['name']])
print(sorted_l_of_dicts) # [{'name': 'Ana', 'years': 19}, {'name': 'Melis', 'years': 38}, {'name': 'John', 'years': 25}, {'name': 'Max', 'years': 18}]

Accesssing all values from same key for different dictionaries within nested dictionary

I have a nested dictionary:
d = { 'wing': {'name': 'Ali', 'age': '19'},
'scrumHalf': {'name': 'Bob', 'age': '25'},
'flyHalf': {'name': 'Sam', 'age': '43'},
'prop': {'name': 'rob', 'age': '33'}}
I want to pull out the values for age only to generate a list
[19, 25, 43, 33]
I want to do this using a for loop, and as naively as possible, as I usually find that easiest to understand.
I have managed to print all of the keys using a for loop:
for i in d:
print i
for j in d[i]:
print j
but when I tried to edit it to print the values I got the error
NameError: name 'value' is not defined. How can I get 'value' to mean the value attached to a key?
Here is my edited version
for i in d:
print (i[value])
for j in d[i]:
print (j[value])
I am using python 2.7
You can access values in the dict with the help of the method values():
[i['age'] for i in d.values()]
# ['19', '25', '43', '33']
>>> [d.get(k).get('age') for k, v in d.items()]
['33', '25', '19', '43']
In-order to access a dictionary's value you are iterating through keys first which is correct i.e. for i in d:. So, in order to access value of key i in d, you'll need to do d[i] which will give you the value, for example {'name': 'rob', 'age': '33'} then to access the required key you'll have to access from dictionary once more i.e. d[i]['age'].

List of tuples to dictionary not working as expected

For some reason my small small brain is having problems with this, I have a list of tuples list = [('name:john','age:25','location:brazil'),('name:terry','age:32','location:acme')]. Im trying to move these values into a dictionary for parsing later. I have made a few attempts, below the latest of these and im not getting all results into the dict, the dict ends up with the last value iterated (its recreating the dict each time).
people = {}
list = [('name:john','age:25','location:brazil'),('name:terry','age:32','location:acme')]
for value in list:
people = {'person': [dict(item.split(":",1) for item in value)]}
You can try this one too:
inlist = [('name:john','age:25','location:brazil'),('name:terry','age:32','location:acme')]
d = []
for tup in inlist:
tempDict = {}
for elem in tup:
elem = elem.split(":")
tempDict.update({elem[0]:elem[1]})
d.append({'person':tempDict})
print(d)
Output:
[{'person': {'location': 'brazil', 'name': 'john', 'age': '25'}}, {'person': {'location': 'acme', 'name': 'terry', 'age': '32'}}]
If you want a dictionary with a key person and values the dictionaries with the people's info, then replace d.append({'person':tempDict}) with d.append(tempDict) and add d = {'person':d} right before printing.
Output:
{'person': [{'location': 'brazil', 'name': 'john', 'age': '25'}, {'location': 'acme', 'name': 'terry', 'age': '32'}]}
You can try this:
l = [('name:john','age:25','location:brazil'),('person:terry','age:32','location:acme')]
people = [{c:d for c, d in [i.split(':') for i in a]} for a in l]
Output:
[{'name': 'john', 'age': '25', 'location': 'brazil'}, {'person': 'terry', 'age': '32', 'location': 'acme'}]
First of all try not to call your list list. This name is protected in python and used usually to get a list out of iterators or ranges etc.
I would make a list of people first and then append each person to the people list as separate dictionary as follows:
people = []
my_list = [('name:john','age:25','location:brazil'),('person:terry','age:32','location:acme')]
for tup in my_list:
person = {}
for item in tup:
splitted = item.split(':')
person.update({splitted[0]:splitted[1]})
people.append(person)
The output then would be this:
[{'age': '25', 'location': 'brazil', 'name': 'john'},
{'age': '32', 'location': 'acme', 'person': 'terry'}]

Categories

Resources