1

So I want to loop over a dictionary and a list simultaneously without them being nested. What I really mean is:

for i,c in enumerate(dictionary) and for k in range(len(list)):
       dictionary[c] = list[k]

So it basically loops over one dictionary and I can assign values to the dictionary with a list.

cs95
  • 379,657
  • 97
  • 704
  • 746
Trollsors
  • 492
  • 4
  • 17

2 Answers2

10

IIUC, you are trying to reassign existing keys to list values. This is something you can only do from python-3.7 onwards (or 3.6 if you use CPython). This can be done either through direct reassignment,

dictionary = dict(zip(dictionary, lst))

Or, if they are not the same length, and there are keys you want to preserve, use dict.update:

dictionary.update(dict(zip(dictionary, lst)))

Additionally, it is unwise to name variables after builtin objects (such as list).

cs95
  • 379,657
  • 97
  • 704
  • 746
  • `{k:v for k,v in }` should probably just be `dict()` – juanpa.arrivillaga Feb 06 '19 at 22:08
  • @juanpa.arrivillaga Thanks. I wanted it to be explicit, but I've added the shorter version as well. – cs95 Feb 06 '19 at 22:08
  • @coldspeed not sure this is what OP is looking for. Based on code, OP wants to have old keys mapped to items in list. But it's not that clear in OP. – Tim Feb 06 '19 at 22:17
  • @Tim The problem with that is that dictionaries aren't ordered. So unless its clear how the list maps to the dictionary, I'm hesitant to change my answer. – cs95 Feb 06 '19 at 22:18
  • @coldspeed I agree, it's not clear how the map should work. Maybe dict is not the right structure for this job at all. But I just read that dict are ordered in python 3.6+. https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6 – Tim Feb 06 '19 at 22:39
  • @Tim Thanks. I am aware, but the question is tagged "python", so there's nothing to suggest that OP is working with ordered dictionaries. I feel like it would be wrong to assume unless they say so. – cs95 Feb 06 '19 at 22:41
  • @coldspeed agreed – Tim Feb 06 '19 at 23:38
  • @coldspeed Ok so what I want to do is I’ve got a list with both odd and even numbers, and I want to sort all odd numbers without changing the position of even numbers. I update a dictionary with the index and value as a key-value pair and add all the odd numbers to a different list and sort them. – Trollsors Feb 07 '19 at 04:41
  • [Contd.] now I want to iterate over both the dictionary and list and update the indices with corresponding values in the list. I hope you understand. – Trollsors Feb 07 '19 at 04:44
  • I know there are better approaches to this but I wanted to understand how do(can) we do this. Also, Thanks for the help. – Trollsors Feb 07 '19 at 04:46
  • @Trollsors You can check this: https://stackoverflow.com/questions/44461172/sort-the-odd-numbers-in-the-list – cs95 Feb 07 '19 at 06:04
  • @Trollsors Also, can you please update your question so it is more clear what you want to do? – cs95 Feb 07 '19 at 06:04
4

zip is your friend

dictionary.update(zip(dictionary, lst))
Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36