1

I need to ignore the last element of a list while using for, without creating any other list or modifying the main list on the process.

And without ignoring the last element if it was repeated in any other position that not the last. So, to ignore only its position.

Here I'm ignoring it, but I had to create 1 extra list:

list1 = [3,2,3] list1_without_last = list1
list1_without_last.remove(list1_without_last[len(list1_without_last)-1])
for i in list1_without_last :
    #do something

I need something like this:

list1 = [3,2,3] for i in list1 except_list1_last_element:

Ex1 : list3 =[4,5,4,5] Output : [4,5,4]

Ex2 : list4 =[1,2,3,4] Output : [1,2,3]

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
Vitor Oliveira
  • 327
  • 2
  • 10

2 Answers2

3

How about this?

list1 = [3, 2, 3]
for i in list1[:-1]:
    print(i)

If not creating any intermediate copies is a hard requirement, you can use itertools.islice:

import itertools
list1 = [3, 2, 3]
for i in itertools.islice(list1, len(list1) - 1)):
    print(i)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Dion
  • 1,492
  • 11
  • 14
2

The canonical way is to access items by index using range. Comparing the index to the length of the list is the simplest way to check it is the last element.

for i in range(len(some_list) - 1):
    item = some_list[i]

An alternate way for iterators which do not support len or indexing is to keep a lookahead copy of the iterator with itertools.tee and stop iteration of the original operator whenever the lookahead reaches the end. This also has the advantage of being straightforward to generalize to drop the last n elements.

from itertools import tee, islice

def drop_last(it, n=1):
    it, lookahead = tee(iter(it))
    next(islice(lookahead, n, n), None)
    for _ in lookahead:
        yield next(it)

for x in drop_last([1, 2, 3]):
    print(x)
# output:
# 1
# 2
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
  • *"And whitout ignore the last element if it was repeated in any other position that not the last."* – Austin Mar 25 '19 at 16:51
  • Isnt it what I am doing? – Olivier Melançon Mar 25 '19 at 16:54
  • I believe last element has to be retained if it somehow repeated elsewhere. – Austin Mar 25 '19 at 16:55
  • 1
    My guess is that this somewhat cryptic statement means that we cannot remove the last element *by value* as it might appear in other positions. But without clarification from the OP we are all just guessing here. – Dion Mar 25 '19 at 16:58
  • @Dion That was my understanding when writing that answer – Olivier Melançon Mar 25 '19 at 16:59
  • I also think that's what OP intended, but that's not what the question specifies. – Prune Mar 25 '19 at 17:06
  • Sorry if the question wasn't clear. Question edited. With " And whitout ignore the last element if it was repeated in any other position that not the last." , i meant to say : ignore only the position . If the pehaps the element in that position was repeated in another position, it should not be ignored. – Vitor Oliveira Mar 25 '19 at 17:38