1

Say I have two lists.

>>> List1 = ['This', 'is', 'a', 'list']
>>> list2 = ['Put', 'this', 'into', 'dictionary']

>>> d = {}

How would I use a loop to make list1 the keys and list2 the values so that each index would store themselves into the dictionary, so as an example...

>>> d = {'This': 'Put', 'is': 'this', 'a': 'into', 'list': 'dictionary'}
Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • 2
    http://stackoverflow.com/questions/209840/map-two-lists-into-a-dictionary-in-python – Sami N Mar 29 '13 at 18:53
  • Do you know about `zip`? If so, this is trivial. If not, go read [the docs](http://docs.python.org/3/library/functions.html#zip), and then it's trivial. – abarnert Mar 29 '13 at 18:59

1 Answers1

1

Since you explicitly ask "How would I use a loop to X" rather than "How do I X", this may be a homework assignment where they don't want you to solve it the trivial way with zip. So, just in case, I'll give you a hint.

First, here's a way to iterate two lists in lockstep (assuming you know they're the same length). It's not the right way to do it—again, that's zip—but if your professor wants you to do things the wrong way, you can.

length = len(List1)
for i in range(length):
    print(List1[i], List2[i])

Now you just have to figure out what to do inside that loop instead of print, do get d[x] to equal y for each matching x and y from the two lists.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Note that this is a *bad* way to do this in general, as `zip()` is right there, this has unstable behaviour for lists of different lengths, it's slow, and it only works on lists, not arbitrary iterables. – Gareth Latty Mar 29 '13 at 19:08
  • So how would I exactly use the zip() function with these lists to put them into a dictionary with the indexes being adjacent for each list? – Tom Manolakis Mar 29 '13 at 19:47
  • @Lattyware: The answer already says, pretty explicitly, that this is the wrong way to do it, and the only reason to do it is if your professor wants you to do things the wrong way. Is that not clear enough? – abarnert Mar 29 '13 at 20:01
  • @TomManolakis: have you read the linked documentation on `zip`, or just done `print(list(zip(List1, List2)))` to see what it does? – abarnert Mar 29 '13 at 20:02
  • @abarnert Unfortunately I regularly see people taking things that clearly say 'don't do it this way', and doing it that way. I tend to therefore try and reinforce it whenever I can - it isn't a judgement on your answer, you were answering the question that was *asked*, and as you say, you mentioned it was bad. I just tried to reinforce that fact by explaining how it is bad. – Gareth Latty Mar 29 '13 at 20:37
  • @TomManolakis Look at the question that this has been marked a duplicate of, the answers there explain it fully. – Gareth Latty Mar 29 '13 at 20:38
  • @Lattyware: Good point. I've got one answer where I said, "If you're both clinically insane and have sub-moronic intelligence, you can even XXX", and then the guy posted a question the next day saying, "So I tried XXX and it doesn't work." So maybe the reinforcement is worth it. – abarnert Mar 29 '13 at 20:45
  • @abarnert Unfortunately, my experience is that degree courses tend to unintentionally teach students that code reuse is bad by disallowing use of the standard library all the time in order to make little code exercises non-trivial. I have seen people doing insane things rather than just using what is already there. As you say, it happens here too, all too often. – Gareth Latty Mar 29 '13 at 20:53