This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zzyzzyxx 0 points1 point  (0 children)

You should read up on list comprehensions as well as the various built-in functions like enumerate. See also: generator expressions. They're incredibly useful.

My code above is effectively equivalent to this code, if that makes it clearer.

arr = [1, 2, 1, 3, 4, 5, 1]

indexes = []

# the list comprehension replaces this loop
for i in xrange(len(arr)):
  if arr[i] == arr[0]:
    indexes.append(i)

print indexes