Home > Software engineering >  I want to convert a list of lists of tuples to list of dictionaries
I want to convert a list of lists of tuples to list of dictionaries

Time:11-18

Input is

[('monday', '09:00:00', '17:00:00'), 
('tuesday', '09:00:00', '17:00:00'), 
('wednesday', '09:00:00', '17:00:00')]

The needed output is

[{'dayOfweek': 'monday', 'time': ['09:00:00', '17:00:00']},
 {'dayOfweek': 'tuesday', 'time': ['09:00:00', '17:00:00']},
  {'dayOfweek': 'wednesday', 'time': ['09:00:00', '17:00:00']}]

I am a beginner in this please help me out.

CodePudding user response:

We will use a list comprehension:

list1=[('monday', '09:00:00', '17:00:00'), 
('tuesday', '09:00:00', '17:00:00'), 
('wednesday', '09:00:00', '17:00:00')]

we create a new_list

new_list=[{'dayOfweek': list1[i][0], 'time': list(list1[i][1:3])} for i in range(len(list1))]

Output

>>> print(new_list)

[{'dayOfweek': 'monday', 'time': ['09:00:00', '17:00:00']},
 {'dayOfweek': 'tuesday', 'time': ['09:00:00', '17:00:00']},
 {'dayOfweek': 'wednesday', 'time': ['09:00:00', '17:00:00']}]

CodePudding user response:

Beginner friendly solution:

tuple_list = [('monday', '09:00:00', '17:00:00'), ('tuesday', '09:00:00', '17:00:00'), ('wednesday', '09:00:00', '17:00:00')]
dict_list = []
for t in tuple_list:
  temp_dict = {}
  temp_dict['dayOfWeek'] = t[0]
  temp_dict['time'] = [t[1], t[2]]
  dict_list.append(temp_dict)

Output:

print(dict_list)

[{'dayOfWeek': 'monday', 'time': ['09:00:00', '17:00:00']},
 {'dayOfWeek': 'tuesday', 'time': ['09:00:00', '17:00:00']},
 {'dayOfWeek': 'wednesday', 'time': ['09:00:00', '17:00:00']}]

CodePudding user response:

Iterate over the list l_in and build the output list l_out.

Each entry is decomposed by using sequence unpacking : day, *time =

Here using a list comprehension a Python mechanism to easily create a list.

l_in = [('monday', '09:00:00', '17:00:00'), ('tuesday', '09:00:00', '17:00:00'), ('wednesday', '09:00:00', '17:00:00')]
l_out = [{"dayOfWeek:": {"day": day, "time": time}} for day, *time in l_in]

Here using a "traditional" loop.

l_in = [('monday', '09:00:00', '17:00:00'), ('tuesday', '09:00:00', '17:00:00'), ('wednesday', '09:00:00', '17:00:00')]
l_out = []
for x in l_in:
    day, *time = x
    l_out.append({"dayOfWeek:": {"day": day, "time": time}})

CodePudding user response:

The concept is to iterate through all input values and breaking them down to constituent parts.

from __future__ import annotations

def convert(input:list[tuple])->list[dict]:
    """
    This function takes in a list of tuples and returns a list of dictionaries

    Use of annotations help you define your input types
    """
    # your output list
    output=[]
    for x in input:
        # dictionary to store each intermediate value
        dict={}
        dict['dayOfweek']=x[0]

        # iterate through the remaining tuples to get the time component
        dict['time']=[t for t in x[1:]]

        # add this dictionary record to your output list
        output.append(dict)

    print(output)
    return output
  • Related