-1

Suppose I have the following:

cell_list = ['B1', 'B2', 'B3']
cell_data = ['1', '2', '3']

How can I build a single loop (presumably, a for loop) with the following result in Python?

B1: 1
B2: 2
B3: 3
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
user2237168
  • 305
  • 1
  • 3
  • 17
  • Don't ask your question in a way that assumes its answer. "How do I have a single loop perform this operation?" is better than "How do I write a `for` loop that performs this operation?" because it makes a wider range of possible answers legitimate. (Granted, in this case, a `for` loop is indeed most likely to be appropriate). – Charles Duffy May 31 '15 at 15:08
  • 1
    (See for instance the great answer by @ZdaR, which was downvoted and self-deleted presumably for not using `for`, despite showing techniques that are very much worth knowing). – Charles Duffy May 31 '15 at 15:09
  • (Also, a question title should be descriptive enough that someone can tell what the operation you're actually trying to perform is, not just what kind of tool you're trying to use for the purpose; I've tried to edit appropriately). – Charles Duffy May 31 '15 at 15:10

5 Answers5

1

The usual way in Python to iterate over 2 or more iterables simultaneously is to use the zip function, which creates a list of tuples. Each tuple in the resulting list contains corresponding elements from each iterable.

cell_list = ['B1', 'B2', 'B3']
cell_data = ['1', '2', '3']

for t in zip(cell_list, cell_data):
    print('%s: %s' % t)

output

B1: 1
B2: 2
B3: 3

If you prefer to use the more modern print syntax, change the print line to this:

print('{0}: {1}'.format(*t))

In versions of Python newer than 2.6, you can write that like this:

print('{}: {}'.format(*t))
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
0

The easiest way would be to zip the lists and apply the dict to the result:

result = dict (zip (cell_list, cell_data))

Having said that, if you absolutely have to use a for loop like the question tags suggest, you could loop over the lists and treat one as potential keys and the other as potential values:

result = {}
for i in range(len(cell_list)):
    result[cell_list[i]] = cell_data[i]
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0
for index in range(len(cell_list)) :
     print cell_list[index] + ": " + cell_data[index] 

Note that this code is not tested as I am posting from my phone.

What this does is it used the range object with the for loop so it will iterate over the values from 0 to the length of your list. Therefore, since your lists are parallel, you can use this index to print the item in both lists.

Paradox
  • 4,602
  • 12
  • 44
  • 88
0
>>> cell_list = ['B1', 'B2', 'B3']
>>> cell_data = ['1', '2', '3']
>>> for a, b in zip(cell_list, cell_data):
       print '{0}: {1}'.format(a, b)


B1: 1
B2: 2
B3: 3
jamylak
  • 128,818
  • 30
  • 231
  • 230
-1

Here is a solution using a for loop:

for i in range(len(cell_list)):
    print (cell_list[i] + ": " + cell_data[i])
  • While this works, it's preferable in Python to iterate directly over iterables rather than using an index, unless you need the numerical value of the index to do some processing. See [Is there a need for range(len(a))?](http://stackoverflow.com/q/19184335/4014959) – PM 2Ring May 31 '15 at 15:25