user3727514
user3727514

Reputation: 273

How can I check a previous index when iterating through a for loop python

I have a list that is somewhat random.

list  = [1,2,4,2,25,3,2,4,2,1,1,32,3,3,2,2,23,4,2,2,2,34,234,33,2,4,3,2,3,4,2,1,3,4,3]

I want to iterate through it and do something like this:

for item in list:
    while item >=5:
        if item = item_5_indexes_ago
            print "its the same as it was 5 entries ago!"

Obviously, item_5_indexes_ago is not valid python. What should I substitute here? I want to check if list[5]==list[1], if list[6]==list[2], ..... for every item in the list.

Upvotes: 3

Views: 14172

Answers (7)

Henrik
Henrik

Reputation: 4314

A pythonic solution is to use the builtin enumerate to keep track of the index as well as the item

for index, item in enumerate(my_list):
    if index >= 5:
        item_5_indexes_ago = my_list[index-5]

Upvotes: 5

lPlant
lPlant

Reputation: 153

The easiest method to do this:

for index, item in enumerate(list):
    if index>=5:
        if item == list[index-5]:
            print "It's the same as it was 5 entries ago!"

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174624

Just step through the list in groups of 6:

from itertools import izip_longest

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

for fives in grouper(biglist, 6):
    if fives[:-1].count(fives[-1]) > 1:
       print("its the same!")

Upvotes: -1

user2357112
user2357112

Reputation: 280778

Iterate over list[5:] and list[:-5] in parallel with zip:

for item, item_5_indices_ago in zip(list[5:], list[:-5]):
    do_whatever_with(item, item_5_indices_ago)

Since zip stops when the shortest input sequence does, you can replace list[:-5] with just list. Also, don't call your list list, or when you try to call the list built-in, you'll get a weird TypeError.

Upvotes: 0

GHugo
GHugo

Reputation: 2654

You can also use list comprehension and enumerate function to get the elements:

[l for i, l in enumerate(list[:-5]) if l == list[i+5]]

Upvotes: 6

Christian Tapia
Christian Tapia

Reputation: 34146

You can loop thorugh indices instead:

for i in range(len(some_list)):
    # do something with
    # list[i]

and to access to a previous element, you can use:

if i >= 4 and list[i] == list[i - 4]:
    print "its the same as it was 4 entries ago!"

Notes:

  • Since you want to check list[5]==list[1] and if list[6]==list[2] it seems like you want to check for an element 4 indices before, not 5.
  • Don't use list as the name of a variable because it will hide its built-in implementation.

Upvotes: 5

Dave Yarwood
Dave Yarwood

Reputation: 3010

A more convenient loop in this case would be to loop by index, like this:

for i in range(len(list)):
    if i >= 5 and list[i] == list[i-5]:
        print "it's the same as it was 5 entries ago!"

Upvotes: 0

Related Questions