-2

I have dictionary with a fixed format like:

dict = {1111: {'vehicle_id': 1111, 'vehicle_capacity': 800},
        3333: {'vehicle_id': 3333, 'vehicle_capacity': 4800}}

The output I would like to get is:

list_dict1 = [{1111: {'vehicle_id': 1111, 'vehicle_capacity': 800}]
list_dict2 = [{3333: {'vehicle_id': 3333, 'vehicle_capacity': 4800}]

The format of dict can't be changed, I tried to get it by calling the keys (1111 and 3333), but the output will show without the keys. Anyone can help me with how to do it?

Doggy Face
  • 357
  • 1
  • 4
  • 12
  • Can the keys of dict be changed and not necessary be 1111 and 3333 – programmer pro Jun 25 '20 at 07:21
  • they can not be changed – Doggy Face Jun 25 '20 at 07:22
  • 2
    Are both, your input and output format, entirely fixed? Because whatever you want to do next will get lists containing one dict of dicts where the subdicts contain the key of the main dict. There is redundancy in the structure and the information content so maybe you can make the next step easier by adapting a reduced data structure? Are you going to generalize this to arbitrarily many keys? How should the resulting lists be stored in that case? – David Wierichs Jun 25 '20 at 07:26

6 Answers6

4

The one-liner solution:

>>> d = {1111: {'vehicle_id': 1111, 'vehicle_capacity': 800},
         3333: {'vehicle_id': 3333, 'vehicle_capacity': 4800}}

>>> lst = [dict([(key, value)]) for key, value in d.items()]
[{1111: {'vehicle_id': 1111, 'vehicle_capacity': 800}}, {3333: {'vehicle_id': 3333, 'vehicle_capacity': 4800}}]

So dict.items() is a simple way access the dictionary items in an iterable format. Then by using list comprehension, it's up to your imagination how you transform it.

Note that dict is a reserved keyword in Python, it's a class constructor to create dictionaries. It's better not to shadow its binding in your code.

viam0Zah
  • 25,949
  • 8
  • 77
  • 100
  • `dict` is a python keyword and as you used it as a variable to define the dictionary doing `dict(..)` will throw an error. – Saad Jun 25 '20 at 07:29
1

This achieves exactly what you ask for, two lists containing one dict each (despite having a different order..)

let's walk through in an interactive python shell. set the variable first:

>>> test = {1111: {'vehicle_id': 1111, 'vehicle_capacity': 800}, 3333: {'vehicle_id': 3333, 'vehicle_capacity': 4800}}
>>> test
{3333: {'vehicle_id': 3333, 'vehicle_capacity': 4800}, 1111: {'vehicle_id': 1111, 'vehicle_capacity': 800}}

Now you can walk the keys in a for loop:

>>> for key in test:
...     print(key)
...
3333
1111

What we now need is to generate the variable names of the lists you want to achieve dynamically. We may use vars() for that. So let's enumerate the keys of the original dict to get a sequence of numbers for each key (you might want to check out zip for similar things as well). The idea is to concatenate the base name of your lists, list_dict, with a sequential integer, num, to generate the variables dynamically. The key in the for loop can be used to set the key of the dict in each list as well as access the original dict to get and assign the corresponding values.

>>> for num, key in enumerate(test):
...     vars()["list_dict" + str(num+1)] = [{key: test[key]}]
...
>>> list_dict1
[{3333: {'vehicle_id': 3333, 'vehicle_capacity': 4800}}]
>>> list_dict2
[{1111: {'vehicle_id': 1111, 'vehicle_capacity': 800}}]
hintze
  • 544
  • 2
  • 13
0

Can you try iterating over the dictionary items then storing the entire dictionary into the lists? Something like

list_dict1 = []
list_dict2 = []
for k, v in dict.items():
    if k == 1111:
        list_dict1.append({k: v})
    else:
        list_dict2.append({k: v})

I'm assuming you're looking for lists of dictionaries.

jyjyj
  • 55
  • 1
  • 7
0

To loop through the dictionary obtaining keys and values, use the following loop:

for key, value in dict.items()

Using this, you can get all required values and keys in a list.

wittn
  • 298
  • 5
  • 16
0
list_all_dict = []
for key, value in dict.items():
    list_cur = []
    list_cur.append({key:value})
    list_all_dict.append(list_cur)

Each of the elements of list all dict is the corresponding list.

xzelda
  • 172
  • 7
0

You can first get the keys of the dict first and then add it to the list.

keys = list(dict1.keys())
list_dict1 = [{keys[0]: dict1[keys[0]]}]
list_dict2 = [{keys[1]: dict1[keys[1]]}]

It will give you the exact same output

programmer pro
  • 169
  • 1
  • 2
  • 12