1

For Example, if I had two lists:

listA = [1, 2, 3, 4, 5]
listB = [red, blue, orange, black, grey]

I'm trying to figure out how to display the elements in the two argument lists in two columns, assigning 1: red, 2: blue... and so on.

This has to be done without using the built-in zipfunction.

theAlse
  • 5,577
  • 11
  • 68
  • 110
Chris Wickell
  • 41
  • 1
  • 1
  • 10
  • 3
    Check out the [zip](https://docs.python.org/2/library/functions.html#zip) function – bruchowski Oct 09 '14 at 02:15
  • @stanleyxu2005, did I miss something about `dict`? Why are you and the other two answers using `dict` here? – John La Rooy Oct 09 '14 at 03:45
  • @gnibbler Oh you are right. His representation `1: red, 2:blue` mislead me to creating a dict. Also he wants the problem being solved without using `zip`. It seems to be kind of a homework question... – stanleyxu2005 Oct 09 '14 at 04:17

5 Answers5

8
 >>> listA = [1, 2, 3, 4, 5]
 >>> listB = ["red", "blue", "orange", "black", "grey"]
 >>> dict(zip(listA, listB))
 {1: 'red', 2: 'blue', 3: 'orange', 4: 'black', 5: 'grey'}
Tangjianke
  • 96
  • 1
  • Unfortunately my teacher does not want us using 'zip' because we haven't gone over it. – Chris Wickell Oct 09 '14 at 02:24
  • 5
    -1 for yor teacher. Discouraging students from reading ahead is not teaching. – John La Rooy Oct 09 '14 at 03:33
  • Why are you converting to a `dict`? The order that the `dict` prints is not guaranteed. – John La Rooy Oct 09 '14 at 03:40
  • For those interested in performance, it seems the `zip` approach is slower than doing this task in a simple `for` loop. In my tests, the `for` loop approach came up twice as fast as the `zip` approach, although the `zip` approach reads more elegantly. – Hassan Baig Aug 17 '20 at 12:14
3

If you can't use zip, do a for loop.

d = {} #Dictionary

listA = [1, 2, 3, 4, 5]
listB = ["red", "blue", "orange", "black", "grey"]

for index in range(min(len(listA),len(listB))):
    # for index number in the length of smallest list
    d[listA[index]] = listB[index]
    # add the value of listA at that place to the dictionary with value of listB

print (d) #Not sure of your Python version, so I've put d in parentheses
Electron
  • 308
  • 4
  • 13
2

Special Teacher edition:

list_a = [1, 2, 3, 4, 5]
list_b = ["red", "blue", "orange", "black", "grey"]

for i in range(min(len(list_a), len(list_b))):
    print list_a[i], list_b[i]
sleeplessnerd
  • 21,853
  • 1
  • 25
  • 29
1

I suspect your teacher wants you to write something like

for i in range(len(listA)):
    print listA[i], listB[i]

However this is an abomination in Python.

Here is one way without using zip

>>> listA = [1, 2, 3, 4, 5]
>>> listB = ["red", "blue", "orange", "black", "grey"]
>>> 
>>> b_iter = iter(listB)
>>> 
>>> for item in listA:
...     print item, next(b_iter)
... 
1 red
2 blue
3 orange
4 black
5 grey

However zip is the natural way to solve this, and your teacher should be teaching you to think that way

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

Usually, zip is the best way to solve your problem. But as it is a homework, and your teacher does not allow you to use zip, I think you can take any solution from this page.

And I provide a version of using a lambda function. Note that if the length of both lists are not identical, a None will be printed at the corresponding place.

>>> list_a = [1, 2, 3, 4, 5]
>>> list_b = ["red", "blue", "orange", "black", "grey"]
>>> for a,b in map(lambda a,b : (a,b), list_a, list_b):
...     print a,b
... 
1 red
2 blue
3 orange
4 black
5 grey
stanleyxu2005
  • 8,081
  • 14
  • 59
  • 94