3

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.

user3727514
  • 273
  • 3
  • 6
  • 14

7 Answers7

6

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]]
GHugo
  • 2,584
  • 13
  • 14
  • Although the list comprehension is nice, there is a lot of information loss here - it's impossible to say for which indices there were matches. I'm not sure if it's what OP needs in this case. – Henrik Jun 11 '14 at 10:09
5

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.
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
5

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]
Henrik
  • 4,254
  • 15
  • 28
1

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!"
Pavling
  • 3,933
  • 1
  • 22
  • 25
lPlant
  • 143
  • 6
0

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!"
Dave Yarwood
  • 2,866
  • 1
  • 17
  • 29
0

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.

user2357112
  • 260,549
  • 28
  • 431
  • 505
-1

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!")
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284