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.

3
  • 3
    Check out the zip function Commented Oct 9, 2014 at 2:15
  • @stanleyxu2005, did I miss something about dict? Why are you and the other two answers using dict here? Commented Oct 9, 2014 at 3: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... Commented Oct 9, 2014 at 4:17

5 Answers 5

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'}
Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately my teacher does not want us using 'zip' because we haven't gone over it.
-1 for yor teacher. Discouraging students from reading ahead is not teaching.
Why are you converting to a dict? The order that the dict prints is not guaranteed.
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.
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

Comments

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]

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.