Home > Net >  How to create a list of lists out of 2 dicts
How to create a list of lists out of 2 dicts

Time:10-19

I have two dictionaries, with the following keys and values:

dict1_keys(['2022-06-10 06:00:00', '2022-06-12 06:00:00', '2022-06-13 06:00:00', '2022-06-14 06:00:00', '2022-06-16 06:00:00', '2022-06-17 06:00:00', '2022-06-18 06:00:00', '2022-06-20 06:00:00', '2022-06-22 06:00:00', '2022-06-29 06:00:00', '2022-06-30 06:00:00', '2022-07-01 06:00:00', '2022-07-02 06:00:00', '2022-07-03 06:00:00', '2022-07-04 06:00:00', '2022-07-05 06:00:00', '2022-07-07 06:00:00', '2022-07-08 06:00:00', '2022-07-09 06:00:00', '2022-07-10 06:00:00', '2022-07-13 06:00:00', '2022-07-14 06:00:00', '2022-07-15 06:00:00', '2022-07-16 06:00:00', '2022-07-18 06:00:00', '2022-07-22 06:00:00', '2022-07-23 06:00:00', '2022-07-25 06:00:00', '2022-07-26 06:00:00', '2022-07-27 06:00:00', '2022-07-29 06:00:00', '2022-07-31 06:00:00', '2022-08-01 06:00:00', '2022-08-02 06:00:00', '2022-08-03 06:00:00', '2022-08-06 06:00:00', '2022-08-10 06:00:00', '2022-08-16 06:00:00', '2022-08-17 06:00:00', '2022-08-20 06:00:00', '2022-08-27 06:00:00', '2022-08-28 06:00:00', '2022-08-30 06:00:00', '2022-08-31 06:00:00', '2022-09-01 06:00:00', '2022-09-03 06:00:00', '2022-09-13 06:00:00', '2022-09-14 06:00:00', '2022-09-15 06:00:00', '2022-09-16 06:00:00', '2022-09-17 06:00:00', '2022-09-18 06:00:00', '2022-09-19 06:00:00', '2022-09-24 06:00:00', '2022-09-26 06:00:00', '2022-09-27 06:00:00', '2022-09-28 06:00:00', '2022-09-29 06:00:00', '2022-09-30 06:00:00', '2022-10-01 06:00:00', '2022-10-02 06:00:00', '2022-10-03 06:00:00', '2022-10-05 06:00:00', '2022-10-07 06:00:00', '2022-10-13 06:00:00', '2022-10-14 06:00:00', '2022-10-15 06:00:00', '2022-10-16 06:00:00'])
dict1_values([2880.0, 4410.0, 4950.0, 0.0, 6450.0, 6600.0, 1410.0, 2340.0, 360.0, 2250.0, 1215.0, 830.0, 750.0, 4080.0, 4070.0, 3000.0, 6000.0, 6300.0, 2460.0, 3488.5714285714284, 0.0, 1320.0, 2640.0, 2250.0, 1840.0, 2640.0, 1170.0, 3412.5, 2826.0, 3840.0, 0.0, 6600.0, 2250.0, 6600.0, 2160.0, 2890.0, 1800.0, 2160.0, 0.0, 1866.6666666666667, 1880.0, 6000.0, 1800.0, 2040.0, 6450.0, 3082.5, 1690.0, 900.0, 1500.0, 1120.0, 2955.0, 3600.0, 1050.0, 720.0, 740.0, 1505.0, 1190.0, 2726.25, 2040.0, 355.0, 4350.0, 2160.0, 1110.0, 1390.0, 4800.0, 895.0, 695.0, 1920.0])
dict2_values([1540.0, 0.0, 2880.0, 850.0, 2640.0, 3300.0, 1725.0, 2445.0, 2040.0, 4800.0, 5400.0, 6900.0, 900.0, 2250.0, 3300.0, 4800.0, 6600.0, 1236.0, 480.0, 3600.0, 1440.0, 384.0, 0.0, 2550.0, 2940.0, 1680.0, 580.0, 0.0, 5100.0, 1896.0, 960.0, 1005.0, 945.0, 540.0, 630.0, 2640.0, 1920.0, 3900.0, 1980.0, 1012.5, 1278.0, 1146.0, 1485.0, 1800.0, 1860.0, 1275.0, 713.3333333333334, 510.0, 1410.0, 1872.0, 2790.0, 250.0, 1560.0, 3900.0, 1360.0, 1590.0, 1620.0, 5700.0, 1240.0, 6000.0, 0.0, 1640.0, 1177.5])

I want to create a list of lists, where every list contains an element of dict1_keys, dict1_values and dict2_values, for example the following

new_list = [["2022-06-10 06:00:00" , 2880.0 , 1540.0] , ["2022-06-12 06:00:00" , 4410.0 , 0.0] , ["2002-06-13 06:00:00" , 4950.0 , 2880.0], .....]

I tried the following list comprehension but got a much longer list than expected

new_list = [[ key1 , value1 , value2 ] for key1, value1 in dict1.items() for key2, value2 in dict2.items() ]

How can I accomplish that? And how can i add None in case the values don't have equal length? I thought that I can add None in this case. Here are the full dicts:

dict1 = {'2022-06-10 06:00:00': 2880.0, '2022-06-12 06:00:00': 4410.0, '2022-06-13 06:00:00': 4950.0, '2022-06-14 06:00:00': 0.0, '2022-06-16 06:00:00': 6450.0, '2022-06-17 06:00:00': 6600.0, '2022-06-18 06:00:00': 1410.0, '2022-06-20 06:00:00': 2340.0, '2022-06-22 06:00:00': 360.0, '2022-06-29 06:00:00': 2250.0, '2022-06-30 06:00:00': 1215.0, '2022-07-01 06:00:00': 830.0, '2022-07-02 06:00:00': 750.0, '2022-07-03 06:00:00': 4080.0, '2022-07-04 06:00:00': 4070.0, '2022-07-05 06:00:00': 3000.0, '2022-07-07 06:00:00': 6000.0, '2022-07-08 06:00:00': 6300.0, '2022-07-09 06:00:00': 2460.0, '2022-07-10 06:00:00': 3488.5714285714284, '2022-07-13 06:00:00': 0.0, '2022-07-14 06:00:00': 1320.0, '2022-07-15 06:00:00': 2640.0, '2022-07-16 06:00:00': 2250.0, '2022-07-18 06:00:00': 1840.0, '2022-07-22 06:00:00': 2640.0, '2022-07-23 06:00:00': 1170.0, '2022-07-25 06:00:00': 3412.5, '2022-07-26 06:00:00': 2826.0, '2022-07-27 06:00:00': 3840.0, '2022-07-29 06:00:00': 0.0, '2022-07-31 06:00:00': 6600.0, '2022-08-01 06:00:00': 2250.0, '2022-08-02 06:00:00': 6600.0, '2022-08-03 06:00:00': 2160.0, '2022-08-06 06:00:00': 2890.0, '2022-08-10 06:00:00': 1800.0, '2022-08-16 06:00:00': 2160.0, '2022-08-17 06:00:00': 0.0, '2022-08-20 06:00:00': 1866.6666666666667, '2022-08-27 06:00:00': 1880.0, '2022-08-28 06:00:00': 6000.0, '2022-08-30 06:00:00': 1800.0, '2022-08-31 06:00:00': 2040.0, '2022-09-01 06:00:00': 6450.0, '2022-09-03 06:00:00': 3082.5, '2022-09-13 06:00:00': 1690.0, '2022-09-14 06:00:00': 900.0, '2022-09-15 06:00:00': 1500.0, '2022-09-16 06:00:00': 1120.0, '2022-09-17 06:00:00': 2955.0, '2022-09-18 06:00:00': 3600.0, '2022-09-19 06:00:00': 1050.0, '2022-09-24 06:00:00': 720.0, '2022-09-26 06:00:00': 740.0, '2022-09-27 06:00:00': 1505.0, '2022-09-28 06:00:00': 1190.0, '2022-09-29 06:00:00': 2726.25, '2022-09-30 06:00:00': 2040.0, '2022-10-01 06:00:00': 355.0, '2022-10-02 06:00:00': 4350.0, '2022-10-03 06:00:00': 2160.0, '2022-10-05 06:00:00': 1110.0, '2022-10-07 06:00:00': 1390.0, '2022-10-13 06:00:00': 4800.0, '2022-10-14 06:00:00': 895.0, '2022-10-15 06:00:00': 695.0, '2022-10-16 06:00:00': 1920.0}.

dict2 = {'2022-06-09 00:00:00': 1540.0, '2022-06-13 00:00:00': 0.0, '2022-06-16 00:00:00': 2880.0, '2022-06-18 00:00:00': 850.0, '2022-06-19 00:00:00': 2640.0, '2022-06-20 00:00:00': 3300.0, '2022-06-22 00:00:00': 1725.0, '2022-06-28 00:00:00': 2445.0, '2022-07-01 00:00:00': 2040.0, '2022-07-04 00:00:00': 4800.0, '2022-07-06 00:00:00': 5400.0, '2022-07-08 00:00:00': 6900.0, '2022-07-14 00:00:00': 900.0, '2022-07-15 00:00:00': 2250.0, '2022-07-16 00:00:00': 3300.0, '2022-07-17 00:00:00': 4800.0, '2022-07-18 00:00:00': 6600.0, '2022-07-22 00:00:00': 1236.0, '2022-07-23 00:00:00': 480.0, '2022-07-24 00:00:00': 3600.0, '2022-07-25 00:00:00': 1440.0, '2022-07-26 00:00:00': 384.0, '2022-07-27 00:00:00': 0.0, '2022-07-29 00:00:00': 2550.0, '2022-07-30 00:00:00': 2940.0, '2022-07-31 00:00:00': 1680.0, '2022-08-01 00:00:00': 580.0, '2022-08-03 00:00:00': 0.0, '2022-08-04 00:00:00': 5100.0, '2022-08-05 00:00:00': 1896.0, '2022-08-06 00:00:00': 960.0, '2022-08-17 00:00:00': 1005.0, '2022-08-19 00:00:00': 945.0, '2022-08-20 00:00:00': 540.0, '2022-08-28 00:00:00': 630.0, '2022-09-03 00:00:00': 2640.0, '2022-09-07 00:00:00': 1920.0, '2022-09-12 00:00:00': 3900.0, '2022-09-13 00:00:00': 1980.0, '2022-09-14 00:00:00': 1012.5, '2022-09-15 00:00:00': 1278.0, '2022-09-16 00:00:00': 1146.0, '2022-09-17 00:00:00': 1485.0, '2022-09-18 00:00:00': 1800.0, '2022-09-19 00:00:00': 1860.0, '2022-09-25 00:00:00': 1275.0, '2022-09-26 00:00:00': 713.3333333333334, '2022-09-27 00:00:00': 510.0, '2022-09-28 00:00:00': 1410.0, '2022-09-29 00:00:00': 1872.0, '2022-09-30 00:00:00': 2790.0, '2022-10-01 00:00:00': 250.0, '2022-10-02 00:00:00': 1560.0, '2022-10-03 00:00:00': 3900.0, '2022-10-04 00:00:00': 1360.0, '2022-10-05 00:00:00': 1590.0, '2022-10-06 00:00:00': 1620.0, '2022-10-09 00:00:00': 5700.0, '2022-10-11 00:00:00': 1240.0, '2022-10-12 00:00:00': 6000.0, '2022-10-13 00:00:00': 0.0, '2022-10-14 00:00:00': 1640.0, '2022-10-16 00:00:00': 1177.5}

CodePudding user response:

I would create a placeholder dictionary with the keys from both dictionaries before constructing the required output with a list comprehension and dict.get.

  1. Dictionary combining the keys (this could also be a list, set or other collection where you get the union of keys from dict1 and dict2):
dict1 = {'2022-06-10': 2880.0, '2022-06-12': 4410.0, '2022-06-13': 4950.0}
dict2 = {'2022-06-09': 1540.0, '2022-06-13': 0.0, '2022-06-16': 2880.0}
d_combined = dict1.copy()
d_combined.update(dict2)
  1. List comprehension (k in d_combined iterates over the keys) and dict.get (which returns none if a key isn't present - a second argument can be provided to change the default return)
list_of_lists = [[k, dict1.get(k), dict2.get(k)] for k in d_combined]

Output:

>>> list_of_lists
[['2022-06-10', 2880.0, None], ['2022-06-12', 4410.0, None], ['2022-06-13', 4950
.0, 0.0], ['2022-06-09', None, 1540.0], ['2022-06-16', None, 2880.0]]

You may want to add an extra step to sort the keys, or sort your list of list afterwards.

CodePudding user response:

First you should add the None values into each dict when they have missing keys:

dict1.update({key: None for key in dict2 if key not in dict1})
dict2.update({key: None for key in dict1 if key not in dict2})

Then, you can just zip them together

new_list = [[key, value1, value2] for (key, value1), value2 in zip(dict1.items(), dict2.values())]

CodePudding user response:

import itertools

list1 = ['2022-06-10 06:00:00', '2022-06-12 06:00:00', '2022-06-13 06:00:00']
list2 = [2880.0, 4410.0, 4950.0]
list3 = [1540.0, 2880.0]


new_list = list(itertools.zip_longest(list1, list2, list3))

new_list = [list(tup) for tup in new_list]

print(new_list)

Output:

[['2022-06-10 06:00:00', 2880.0, 1540.0], 
['2022-06-12 06:00:00', 4410.0, 2880.0], 
['2022-06-13 06:00:00', 4950.0, None]]

EDTI: native solution

Add None to the values to match the length of list1 so you can use zip

list1 = ['2022-06-10 06:00:00', '2022-06-12 06:00:00', '2022-06-13 06:00:00']
list2 = [2880.0, 4410.0, 4950.0]
list3 = [1540.0, 2880.0]

new_list = []

if len(list1) > len(list2):
    for i in range(len(list2), len(list1)):
        list2.append(None)
        
if len(list1) > len(list3):
    for i in range(len(list3), len(list1)):
        list3.append(None)
        
new_list = [[i, j, k] for (i, j, k) in zip(list1, list2, list3)]

print(new_list)
  • Related