user974703
user974703

Reputation: 1653

for if comparsion

I'm new to python and I'm having a bit of trouble understanding how this is supposed to work.

Lets say i have a for loop like so :

for row in rows
    if row["Title"] == List[row] 
         do something

gives me the error that list indicies must be integers.

if I'm cycling through every row, how can I represent the current row I'm at as an index?

Upvotes: 1

Views: 111

Answers (4)

Derek Litz
Derek Litz

Reputation: 10897

There are some important things you should understand and you may be doing wrong based on the code sample you've provided so far.

  1. You're trying to use an element in rows as a index. If you expected this to be an integer you may need to convert it (ie. if it's a string like '1').

  2. In addition to point 1, You're using row twice for two different things. The other looks like you're expecting it to be a dictionary. You could potentially be overwriting another row variable at a higher scope. See example at bottom as I can't seem to format well in the bullet point...

  3. If the code below #Do Something manipulates rows, this could result in disastrous consequences. For example, the code below is an infinite loop.

    for element in list1: list1.append(element - 5)

    Instead you should use, which makes a copy of list1 using slice notation for iteration and you can modify the copy you are not iterating over.:

    for element in list1[:]: list1.append(element - 5)

  4. You're accessing a different list based on index and you aren't iterating over it. You could easily get an IndexError doing this. If you want to iterate over two lists do it like this or use the itertools equivalent. This way you are safe, but you do need to make certain the lists are of the same length.

    for element1, element2, in zip(list1, list2): pass

Others are telling you to use enumerate... without actually looking at your code this does answer your question, but it's just sugar and a common Python idiom. Here's how it works.

help(enumerate)
Help on class enumerate in module __builtin__:

class enumerate(object)
 |  enumerate(iterable[, start]) -> iterator for index, value of iterable
 |  
 |  Return an enumerate object.  iterable must be another object that supports
 |  iteration.  The enumerate object yields pairs containing a count (from
 |  start, which defaults to zero) and a value yielded by the iterable argument.
 |  enumerate is useful for obtaining an indexed list:
 |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...

I'm sure you could imagine other ways of achieving the same thing.

Example for point 2 above.

In [71]: list1
Out[71]: [1, 2, 3, 4, 5, -4, -3, -2, -1, 0]

In [72]: x = 5

In [73]: for x in list1:
    ...:     pass

In [74]: x
Out[74]: 0

Upvotes: 4

Blender
Blender

Reputation: 298206

You can enumerate() it:

for index, row in enumerate(rows):
  if row['Title'] == List[index]:
    # Do something

Upvotes: 3

middus
middus

Reputation: 9121

for i, row in enumerate(rows):
    # do something

Upvotes: 3

Fred Foo
Fred Foo

Reputation: 363607

Use the built-in function enumerate.

for i, row in enumerate(rows):

Upvotes: 1

Related Questions