2 Replies - 487 Views - Last Post: 17 November 2014 - 11:50 AM Rate Topic: -----

#1 xmcgruffx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-November 14

Find index in a for loop

Posted 17 November 2014 - 11:29 AM


I'm learning python and I'm stuck at an assignment. This is also my very first post on this forum.

For example:
list1 = ['1', '2', '3', '4', '5']
list2 = ['a', 'b', 'c', 'd', 'e']

for i in list1:
    print i
    print ... #Item from list2

How would I print from list2 the item in the list corresponding with the index position of i in the for-loop? Also, is there a way to know the position of i in a for loop?

Sorry for any bad English, it's not my native language. I appreciate any help I can get.

Is This A Good Question/Topic? 0
  • +

Replies To: Find index in a for loop

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1509
  • View blog
  • Posts: 8,273
  • Joined: 07-September 06

Re: Find index in a for loop

Posted 17 November 2014 - 11:48 AM

What you really want to do is loop over the numbers from 0 to len(list1) to get the indexes for each element in i instead. This will allow you to print both lists at the same time without having multiple loops. A simple example (not using lists, but will help out):

for i in range(0, 10):
    print i

range takes a starting and ending number (0 and 10 here respectively) and returns an array of elements from the start to 1 less than the end (via python shell -- idle):
>>> range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

So, now if you use len to find the length of your array and use that instead of the end you should be able to loop through the indexes in your arrays.

Hopefully that makes sense.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is online

  • Beginner
  • member icon

Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Find index in a for loop

Posted 17 November 2014 - 11:50 AM

Probably the easiest way to get the item from list2 corresponding to the index listed in list1 would be list2[str(list1[i])]. This gets a string from list1, converts it to an int (since a string is not a valid list index) and does the lookup. However, that's not going to work the way you want it to, since there is no list2[5] (list2's indices go from 0..4)
Another thing that you might mean would be:

for (i,j) in zip(list1, list2):
  print "%s and %s are at the same positions in list1 and list2" %(i,j)

The zip() function takes two iterables i1 and i2, and returns a list of pairs of items from those lists:

>>> l1 = [1,2,3,4,5]
>>> l2 = ['a','b','c','d','e']
>>> zip(l1,l2)
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]

There are two ways to get the current position in a loop. One would be to make a more traditional C-style for loop:

for i in range(len(list1)):
  # here you can look up list1[i] and do what you want with it

Most people writing python would prefer to write

for (i, item) in enumerate(list1):
  # here you can point to the index as i or the item as item

Of course, in python, you don't usually want to have to care about the position of an item in a list - python typically tries to focus your attention on the things you care about, and where the item lives in the list is not usually one of those things.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1