Tessa
Tessa

Reputation: 19

how to convert set of dictionaries to a list of dictionaries?

i am trying to convert my individual separate dictionaries into a list of dictionaries

this is my current dictionary:

order = {'company_name': 'FRIENDS',  'delivery_timeslot': '18-21', 'latitude': '1.37315509207642000000', 'longitude': '103.76570152476201000000', 'status': 'pending', 'date': '2019-12-06', 'region': 'WEST'}
{'company_name': 'FLOWERS', 'delivery_timeslot': '18-21', 'latitude': '1.28821802835873000000', 'longitude': '103.84569230314800000000', 'status': 'pending', 'date': '2019-11-29', 'region': 'CENTRAL'}
{'company_name': 'SUNSHINE',  'delivery_timeslot': '18-21', 'latitude': '1.37414901860683000000', 'longitude': '103.94353973518100000000', 'status': 'pending', 'date': '2019-11-08', 'region': 'EAST'}
{'company_name': 'CLOUDS', 'delivery_timeslot': '18-21', 'latitude': '1.39663901543996000000', 'longitude': '103.81937713764700000000', 'status': 'pending', 'date': '2019-10-25', 'region': 'NORTH'}
{'company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH'}

this is my intended output:

order = [{'company_name': 'FRIENDS',  'delivery_timeslot': '18-21', 'latitude': '1.37315509207642000000', 'longitude': '103.76570152476201000000', 'status': 'pending', 'date': '2019-12-06', 'region': 'WEST'},
{'company_name': 'FLOWERS', 'delivery_timeslot': '18-21', 'latitude': '1.28821802835873000000', 'longitude': '103.84569230314800000000', 'status': 'pending', 'date': '2019-11-29', 'region': 'CENTRAL'},
{'company_name': 'SUNSHINE',  'delivery_timeslot': '18-21', 'latitude': '1.37414901860683000000', 'longitude': '103.94353973518100000000', 'status': 'pending', 'date': '2019-11-08', 'region': 'EAST'},
{'company_name': 'CLOUDS', 'delivery_timeslot': '18-21', 'latitude': '1.39663901543996000000', 'longitude': '103.81937713764700000000', 'status': 'pending', 'date': '2019-10-25', 'region': 'NORTH'},
{'company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH'}]

i have tried:

neworder = list(order.items())

but this only returns one dataset

[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]

then i tried a loop:

for i in order:
    neworder = list(order.items()) 
    print(neworder)

but it still only returns 1 dataset and multiple lists

[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]

Upvotes: 0

Views: 24

Answers (1)

Dev Khadka
Dev Khadka

Reputation: 5451

from where do you get your set of dictionaries, if you are initializing order in you code like this then you have only assigned 1st dictionary to order other are not assigned to any variable. you should wrap the dictionaries inside "[ ]" and add commas like below

order = [
    {'company_name': 'FRIENDS',  'delivery_timeslot': '18-21', 'latitude': '1.37315509207642000000', 'longitude': '103.76570152476201000000', 'status': 'pending', 'date': '2019-12-06', 'region': 'WEST'}
    {'company_name': 'FLOWERS', 'delivery_timeslot': '18-21', 'latitude': '1.28821802835873000000', 'longitude': '103.84569230314800000000', 'status': 'pending', 'date': '2019-11-29', 'region': 'CENTRAL'},
    {'company_name': 'SUNSHINE',  'delivery_timeslot': '18-21', 'latitude': '1.37414901860683000000', 'longitude': '103.94353973518100000000', 'status': 'pending', 'date': '2019-11-08', 'region': 'EAST'},
    {'company_name': 'CLOUDS', 'delivery_timeslot': '18-21', 'latitude': '1.39663901543996000000', 'longitude': '103.81937713764700000000', 'status': 'pending', 'date': '2019-10-25', 'region': 'NORTH'},
    {'company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH'}
]

Upvotes: 1

Related Questions