0

This is an example of what I am trying to do.

for (k,v), (k2,v2) in zip(name_to_phone.items(), name_to_address.items()):
        if  v2 or k2 not in address_to_all:
            address_to_all[v2] = ([].append(k),v)

But the list doesn't show up in the tuple when I print it, it only says none. How can I fix this? Edit: These are the dictionaries:

name_to_phone = {'alice': 5678982231, 'bob': '111-234-5678', 'christine': 5556412237, 'daniel': '959-201-3198', 'edward': 5678982231}
name_to_address = {'alice': '11 hillview ave', 'bob': '25 arbor way', 'christine': '11 hillview ave', 'daniel': '180 ways court', 'edward': '11 hillview ave'}

1 Answers1

1

I came up with this dictionary comprehension.

for (k,v), (k2,v2) in zip(name_to_phone.items(), name_to_address.items()):
   address_to_all.update({v2:([key for key,value in name_to_address.items() if value == v2],v)})


Out: {'11 hillview ave': (['alice', 'christine', 'edward'], 5678982231), '25 arbor way': (['bob'], '111-234-5678'), '180 ways court': (['daniel'], '959-201-3198')}
Yagiz Degirmenci
  • 16,595
  • 7
  • 65
  • 85
  • Thank you. Could you explain why this worked and my expression didn't. To me it is basically supposed to be doing the same thing. – lameprogrammer01 Jun 24 '20 at 20:46
  • `append` function mutates the list and returns `None` so when you say something like `address_to_all[v2] = ([].append(k), v)` you are actually assigning `address_to_all[v2]` with `None` so the next time when you refer that list its actually pointing the `None` not the list that's why something like `[].append(k),v` wont work – Yagiz Degirmenci Jun 24 '20 at 20:56