0

I have two dicts through which I am creating two different lists and what I want is for them to be appended to a list without merging them so that I perform the operations on each list while iterating.

  • Each list contains two slots which I need to use to run a command in order to blink a led on and off .
  • I need the final output to be something like this : { 'first' : [slot1, slot2], 'second' : [slot1, slot2] } or simply { [[slot1, slot2], [slot1, slot2]] }
first_dict = {'address1': [1], 'address1': [2]}
second_dict = {'address2': [3], 'address2': [4]}
outlet_list1 = []
for x, slot in first_dict.items():
    outlet_list1.extend(slot)
outlet_list2 = []
for x, slot in second_dict.items():
    outlet_list2.extend(slot)

Can someone help to optimize and provide a solution for the mentionned problem ?

  • 2
    It's hard to tell what your code is actually doing (a [mre] would help a lot) but there's a good chance that `first_dict`, `second_dict`, and `obj.ctrl_dict` are all the same object. I guess that's not what you want? – ChrisGPT was on strike Jul 25 '21 at 16:29
  • Also, please read [ask]. Your question could benefit from additional clarity. – ChrisGPT was on strike Jul 25 '21 at 16:30
  • @Chris I have edited the code snipet –  Jul 25 '21 at 16:33
  • See [List changes unexpectedly after assignment. Why is this and how can I prevent it?](https://stackoverflow.com/q/2612802/354577), for example. That's for lists, not dicts, but it applies here too. – ChrisGPT was on strike Jul 25 '21 at 16:34
  • Thank you for updating your code, but it still isn't reproducible. What is `obj`, and what are its `ctrl_dict1` and `ctrl_dict2` properties? – ChrisGPT was on strike Jul 25 '21 at 16:35
  • what does your input ctrl dics look like? – devReddit Jul 25 '21 at 16:35
  • 1
    Write a separate test script that fully demonstrates the problem. We don't know your specific problem space. `first_dict = obj.ctrl_dict1` is what? Instead, just initialize `first_dict = {'mac_address': [19], 'mac_address': [19]}` and leave obj out of it. Also, I have no idea what "append without merge" means. Better to show both the output you get and the output you want. – tdelaney Jul 25 '21 at 16:42
  • also, what is `{'mac_address': [19], 'mac_address': [19]}`? Is that even a dict with two keys? Isn't it same as `{'mac_address': [19]}` only? – devReddit Jul 25 '21 at 16:48
  • @devReddit I have edited the code. address is common for one dict. The slots would be different or might be same as well. I need to pass the slots in a function which takes input as a string like "1,2" which would turn off and on slots 1 and 2. So, I need the slots based on the first dict and second dict and pass it to that function one by one. Does that clarify? –  Jul 25 '21 at 17:02
  • You say "address is common for one dict" but dict keys are unique. If you do `{'address1': [1], 'address1': [2]}`, that second "address1" overwrites the first one and you end up with a dict with only 1 thing in it. – tdelaney Jul 25 '21 at 17:15

2 Answers2

0
first_dict = {'1':1, '2':2}
second_dict = {'1':1, '2':2}
outlet_list1 = []
outlet_dict = {}

outlet_list1.append(first_dict)
outlet_list1.append(second_dict)
print(outlet_list1)

########## OR ##########

outlet_dict['First'] = first_dict
outlet_dict['Second'] = second_dict
print(outlet_dict)
0

As per your requirement you mentioned above, you can do the job simple writing the list flattening one liner for individual lists:

first_dict = {'address1': [1], 'address2': [2]}
second_dict = {'address1': [3], 'address2': [4]}

outlet_list1 = [item for sublist in first_dict.values() for item in sublist]
outlet_list2 = [item for sublist in second_dict.values() for item in sublist]

result=[outlet_list1, outlet_list2]

print(result)

That gives you:

[[1, 2], [3, 4]]

The one liner basically means:

for sublist in first_dict.values():
    for item in sublist:
        outlet_list1.append(item)

Hope that's answer your question properly.

MORE ORGANIZED SOLUTION

If you have a list of dicts, you can do the job as follows:

first_dict = {'address1': [1], 'address2': [2]}
second_dict = {'address1': [3], 'address2': [4]}
list_of_dict = [first_dict, second_dict]

result = []

for dict_item in list_of_dict:
    result.append([item for sublist in dict_item.values() for item in sublist])

print(result)

You'll get the same.

devReddit
  • 2,696
  • 1
  • 5
  • 20