Home > Software design >  Convert list of tuples and list to dictionary
Convert list of tuples and list to dictionary

Time:09-17

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}}

CodePudding user response:

Try this:

dict(zip(title, data))

Or for making them sets:

dict(zip(title, map(set, data)))

CodePudding user response:

Try :

output = {}
output["Date"] = set(data[0])
output["Name"] = set(data[1])
output["Age"] = set(data[2])

CodePudding user response:

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)}

CodePudding user response:

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'}}
  • Related