I have nested list which looks something like this
my_list = [[1,'raj','CSE'],[2,'kumar','MECH'],[3,'Nisha','ECE']]
Since I need to export this in CSV I want to convert this into a dictionary. My output should be like this.
my_dict = {'id':[1,2,3],'Name':['raj','kumar','Nisha'],'Course':['CSE','MECH','ECE']}
How can I achieve this???
Easily done with zip:
l = [[1,'raj','CSE'],[2,'kumar','MECH'],[3,'Nisha','ECE']]
d = dict(zip(['Id', 'Name', 'Course'], map(list, (zip(*l)))))
d
# {'Course': ['CSE', 'MECH', 'ECE'],
# 'Id': [1, 2, 3],
# 'Name': ['raj', 'kumar', 'Nisha']}
Since you want to convert it to a dict first before saving it to a csv, I'm assuming you use pandas (otherwise it'd have been easier to save it in its existing form). This is easily done:
df = pd.DataFrame(d) # `d` is from the code snippet above.
df
Course Id Name
0 CSE 1 raj
1 MECH 2 kumar
2 ECE 3 Nisha
df.to_csv('test.csv')
Alternatively, if you don't want to use pandas, just do this:
l = [[1,'raj','CSE'],[2,'kumar','MECH'],[3,'Nisha','ECE']]
with open('test.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(['Id', 'Name', 'Course'])
writer.writerows(l)
In this situation, you do not require conversion to a dictionary.
An alternative to #Coldspeed's very elegant solution:
headers = ['id', 'Name', 'Course']
my_list = [[1,'raj','CSE'],[2,'kumar','MECH'],[3,'Nisha','ECE']]
my_dict = {k: [x[i] for x in my_list] for i, k in enumerate(headers)}
print(my_dict) # {'Name': ['raj', 'kumar', 'Nisha'], 'Course': ['CSE', 'MECH', 'ECE'], 'id': [1, 2, 3]}
If you want just to export it to csv, Pandas is quite handy:
import pandas as pd
pd.DataFrame(my_list, columns=['id','Name','Course']).to_csv("Name.csv")
A simple and elegant way to me is by zip function:
my_list = [[1,'raj','CSE'],[2,'kumar','MECH'],[3,'Nisha','ECE']]
ids, names, courses = map(list, zip(*my_list))
my_dict = {'id': ids, 'Name': names, 'Course': courses}
The answer is simple:
my_dict={
'id':[a[0] for a in my_list],
'Name': [a[1] for a in my_list],
'Course':[a[2] for a in my_list]
}
Related
I have a list of tuples
data = [(2015-10-08,2016-07-17,2015-09-09,2014-01-29),(Alex, Jerry, Tony, Tom), (5,6,7,8)]
And I have a list, this list contains column headings.
title = [Date , Name, Age]
With this list and list of tuples I want a dictionary.
This is the expected output
output = {1:{'Date': 2015-10-08,2016-07-17,2015-09-09,2014-01-29} ,{'Name' : Alex, Jerry, Tony, Tom}, {'Age' : 5,6,7,8}}
Try this:
dict(zip(title, data))
Or for making them sets:
dict(zip(title, map(set, data)))
Try :
output = {}
output["Date"] = set(data[0])
output["Name"] = set(data[1])
output["Age"] = set(data[2])
data = [('2015-10-08','2016-07-17,2015-09-09','2014-01-29'),('Alex', 'Jerry', 'Tony', 'Tom'), (5,6,7,8)]
title = ['Date', 'Name', 'Age']
your_dict = {key: value for key, value in zip(title, data)}
Here is one of the solutions:
data = [('2015-10-08','2016-07-17','2015-09-09','2014-01-29'),('Alex', 'Jerry', 'Tony', 'Tom'), ('5','6','7','8')]
title = ['Date', 'Name', 'Age']
output = {}
for i in range(len(title)):
output[i+1] = {title[i]: ",".join(data[index])}
print (output)
Output:
{1: {'Date': '2015-10-08,2016-07-17,2015-09-09,2014-01-29'}, 2: {'Name': '2015-10-08,2016-07-17,2015-09-09,2014-01-29'}, 3: {'Age': '2015-10-08,2016-07-17,2015-09-09,2014-01-29'}}
I have a list of dictionaries with multiple KVP each
list_dict = [{'id': 1, 'name': 'sana'}, {'id': 2, 'name': 'art'}, {'id': 3, 'name': 'tiara'}]
I want to transform this into this format:
final_dict = {1: 'sana', 2: 'art', 3: 'tiara'}
I've been trying dict comprehensions but it does not work. Here's the best that I could do:
{k:v for d in list_dict for k, v in d.items()}
You don't need d.items(), you can just access the id and name properties of each dict.
{d['id']: d['name'] for d in list_dict}
for each element of the list you want the d["id"] to be the key and d["name"] to be the value, so the dictionary comprehension would look like this:
{d["id"]: d["name"] for d in list_dict}
You can try
final_dict={}
for dico in list_dict:
final_dict[dico['id']] = dico['name']
There's probably a few different ways you can do this. Here's a nice simple way of doing it using a pandas dataframe:
import pandas as pd
df = pd.DataFrame(list_dict) # <- convert to dataframe
df = df.set_index('id') # <- set the index to the field you want as the key
final_dict = df['name'].to_dict() # <- convert the series 'name' to a dict
print(final_dict)
{1: 'sana', 2: 'art', 3: 'tiara'}
I have a dictionary which is converted from a dataframe as below :
a = d.to_json(orient='index')
Dictionary :
{"0":{"yr":2017,"PKID":"58306, 57011","Subject":"ABC","ID":"T001"},"1":{"yr":2018,"PKID":"1234,54321","Subject":"XYZ","ID":"T002"}}
What I need is it be in a list, so essentially a list of dictionary.
So i just add a [] because that is the format to be used in the rest of the code.
input_dict = [a]
input_dict :
['
{"0":{"yr":2017,"PKID":"58306, 57011","Subject":"ABC","ID":"T001"},"1":{"yr":2018,"PKID":"1234,54321","Subject":"XYZ","ID":"T002"}}
']
I need to get the single quotes removed just after the [ and just before the ]. Also, have the PKID values in form of list.
How can this be achieved ?
Expected Output :
[ {"yr":2017,"PKID":[58306, 57011],"Subject":"ABC","ID":"T001"},"1":{"yr":2018,"PKID":[1234,54321],"Subject":"XYZ","ID":"T002"} ]
NOTE : The PKID column has multiple integer values which have to come as a lift of integers. a string is not acceptable.
so we need like "PKID":[58306, 57011] and not "PKID":"[58306, 57011]"
pandas.DataFrame.to_json returns a string (JSON string), not a dictionary. Try to_dict instead:
>>> df
col1 col2
0 1 3
1 2 4
>>> [df.to_dict(orient='index')]
[{0: {'col1': 1, 'col2': 3}, 1: {'col1': 2, 'col2': 4}}]
>>> df.to_dict(orient='records')
[{'col1': 1, 'col2': 3}, {'col1': 2, 'col2': 4}]
Here is one way:
from collections import OrderedDict
d = {"0":{"yr":2017,"PKID":"58306, 57011","Subject":"ABC","ID":"T001"},"1":{"yr":2018,"PKID":"1234,54321","Subject":"XYZ","ID":"T002"}}
list(OrderedDict(sorted(d.items())).values())
# [{'ID': 'T001', 'PKID': '58306, 57011', 'Subject': 'ABC', 'yr': 2017},
# {'ID': 'T002', 'PKID': '1234,54321', 'Subject': 'XYZ', 'yr': 2018}]
Note the ordered dictionary is ordered by text string keys, as supplied. You may wish to convert these to integers first before any processing via d = {int(k): v for k, v in d.items()}.
You are converting your dictionary to json which is a string. Then you wrap your resulting string a list. So, naturally, the result is a string inside of a list.
Try instead: [d] where d is your raw dictionary (not converted json
You can use a list comprehension
Ex:
d = {"0":{"yr":2017,"PKID":"58306, 57011","Subject":"ABC","ID":"T001"},"1":{"yr":2018,"PKID":"1234,54321","Subject":"XYZ","ID":"T002"}}
print [{k: v} for k, v in d.items()]
Output:
[{'1': {'PKID': '1234,54321', 'yr': 2018, 'ID': 'T002', 'Subject': 'XYZ'}}, {'0': {'PKID': '58306, 57011', 'yr': 2017, 'ID': 'T001', 'Subject': 'ABC'}}]
What about something like this:
from operator import itemgetter
d = {"0":{"yr":2017,"PKID":"58306, 57011","Subject":"ABC","ID":"T001"},"1":
{"yr":2018,"PKID":"1234,54321","Subject":"XYZ","ID":"T002"}}
sorted_d = sorted(d.items(), key=lambda x: int(x[0]))
print(list(map(itemgetter(1), sorted_d)))
Which Outputs:
[{'yr': 2017, 'PKID': '58306, 57011', 'Subject': 'ABC', 'ID': 'T001'},
{'yr': 2018, 'PKID': '1234,54321', 'Subject': 'XYZ', 'ID': 'T002'}]
i have a python list of dictionary as shown below:
mylist = [{'id':1,'value':4},{'id':1,'value':6},{'id':2,'value':6},{'id':3,'value':9},{'id':3,'value':56},{'id':3,'value':67},]
i am trying to create a new list of dictionaries like this by doing some operations on the above shown list of dictionaries
newlist = [{'id':1,'value':[4,6]},{'id':2,'value':[6]},{'id':3,'value':[9,56,67]}]
Does anyone know a good way to do this?
If list items are sorted by id, you can use itertools.groupby:
>>> mylist = [{'id':1,'value':4},{'id':1,'value':6},{'id':2,'value':6},{'id':3,'value':9},{'id':3,'value':56},{'id':3,'v alue':67},]
>>> import itertools
>>> [{'id': key, 'value': [x['value'] for x in grp]}
... for key, grp in itertools.groupby(mylist, key=lambda d: d['id'])]
[{'id': 1, 'value': [4, 6]},
{'id': 2, 'value': [6]},
{'id': 3, 'value': [9, 56, 67]}]
You can construct the entire the list of dictionaries as a single dictionary with multiple values, using defaultdict, like this
from collections import defaultdict
d = defaultdict(list)
for item in mylist:
d[item['id']].append(item['value'])
And then using list comprehension, you can reconstruct the required list of dictionaries like this
print[{'id': key, 'value': d[key]} for key in d]
# [{'id':1, 'value':[4, 6]}, {'id':2, 'value':[6]}, {'id':3, 'value':[9,56,67]}]
You could also use dict comprehension:
newlist = {key: [entries[key] for entries in diclist] for key, value in diclist[0].items()}
I am trying to take two lists, each of which are lists of dictionaries with the same keys, and output versions of each list that only contain dictionaries that share common values for one of the keys. For example:
#before:
json1 = [{'id':1, 'name':'john', 'age': 3}, {'id':2, 'name':'jack', 'age':5}]
json2 = [{'id':3, 'name':'john', 'age': 5}, {'id':1, 'name':'jill', 'age':3}]
#Do some operation that merges based on the key 'id'
json1 = [{'id':1, 'name':'john', 'age': 3}]
json2 = [{'id':1, 'name':'jill', 'age':3}]
So, merging the lists of dicts based on id would output what I wrote above. Merging based on another key, say 'name', would only keep the first dict of each list.
Does anyone know a good way to do this?
EDIT
Sorry about the list names, I guess to be extremely accurate I'll call them json1 and json2
I thing your merging function could be something like that
def merge(key, l1, l2):
k1 = { d[key] for d in l1 }
k2 = { d[key] for d in l2 }
keys = k1.intersection(k2)
f1 = [ d for d in l1 if d[key] in keys ]
f2 = [ d for d in l2 if d[key] in keys ]
return f1, f2
That is :
take values of the key used for merging (in your examples 'id' or 'name')
in sets to avoid duplicates
find common values in the 2 sets
keep only dicts from the initial lists where the key take one of the common values
If you take merge('id', json1, json2) you get a 2-tuple of your resulting json1 and json2
Assuming I understand you, I'd do this in two passes: first find the common values, and then build the new lists:
>>> j1 = [{'id':1, 'name':'john', 'age': 3}, {'id':2, 'name':'jack', 'age':5}]
>>> j2 = [{'id':3, 'name':'john', 'age': 5}, {'id':1, 'name':'jill', 'age':3}]
>>> jj = (j1, j2)
>>> common = set.intersection(*({d['id'] for d in j} for j in jj))
>>> common
set([1])
>>> jjnew = [[d for d in j if d['id'] in common] for j in jj]
>>> jjnew
[[{'age': 3, 'id': 1, 'name': 'john'}], [{'age': 3, 'id': 1, 'name': 'jill'}]]
And similarly for name:
>>> common = set.intersection(*({d['name'] for d in j} for j in jj))
>>> jjnew = [[d for d in j if d['name'] in common] for j in jj]
>>> jjnew
[[{'age': 3, 'id': 1, 'name': 'john'}], [{'age': 5, 'id': 3, 'name': 'john'}]]