3

I have a list for example: list_a = [0,1,3,1]

and I am trying to iterate through each number this loop, and if it hits the last "1" in the list, print "this is the last number in the list"

since there are two 1's, what is a way to access the last 1 in the list?

I tried:

 if list_a[-1] == 1:
      print "this is the last"  
   else:
     # not the last

This does not work since the second element is also a 1. Tried:

if list_a.index(3) == list_a[i] is True:
   print "this is the last"

also did not work, since there are two 1's

6
  • 1
    Are you looking for the last item, or for the last 1? They're not necessarily the same thing.
    – askewchan
    Commented Mar 15, 2013 at 19:40
  • 2
    So what do you want? Accessing the last number or the last 1?
    – chtenb
    Commented Mar 15, 2013 at 19:40
  • 1
    the last number, not the last 1
    – orpqK
    Commented Mar 15, 2013 at 19:42
  • The thing is, in Python, you just can't distinguish between those two 1, they are the same object and both list positions point to the same object. Commented Mar 15, 2013 at 19:43
  • 1
    How did your first solution not work? I surely print out this is the last. Should it print this sentence if the last number is 1? If so, it works.
    – brandizzi
    Commented Mar 15, 2013 at 19:44

5 Answers 5

19

list_a[-1] is the way to access the last element

5
  • the question explicity states that that didn't work, consider remove this answer as it is wrong Commented Mar 15, 2013 at 19:41
  • 2
    @F.C. The questions says that comparing the last element to 1 didn't work: list_a[-1] == 1, which is not the same thing as this answer.
    – askewchan
    Commented Mar 15, 2013 at 19:42
  • @F.C. Come on Man! You know that in lists list_a[-1] always refers to the last element. The OP is confused on this as there are two elements with the same value in the list. This does not mean that list_a[-1] is wrong. Just that we need to re-affirm to the OP saying that he should not be confused and that list_a[-1] is the correct way to access the last element
    – GodMan
    Commented Mar 15, 2013 at 19:43
  • 1
    @GodMan ok, maybe you should add some explanation to your answer, I don't think it would help much as it is. Commented Mar 15, 2013 at 19:46
  • i would like a func like next(iter(array), None) somthing like last(iter(array), None) this way i dont get error if is an empty array Commented Nov 15, 2023 at 20:49
7

You can use enumerate to iterate through both the items in the list, and the indices of those items.

for idx, item in enumerate(list_a):
    if idx == len(list_a) - 1:
        print item, "is the last"
    else:
        print item, "is not the last"

Result:

0 is not the last
1 is not the last
3 is not the last
1 is the last
2

Tested on Python 2.7.3

This solution will work for any sized list.

list_a = [0,1,3,1]

^ We define list_a

last = (len(list_a) - 1)

^We count the number of elements in the list and subtract 1. This is the coordinate of the last element.

print "The last variable in this list is", list_a[last]

^We display the information.

1
  • @askewchan Thanks so much - didn't notice that! Commented Mar 15, 2013 at 19:56
0
a = [1, 2, 3, 4, 1, 'not a number']
index_of_last_number = 0

for index, item in enumerate(a):
    if type(item) == type(2):
        index_of_last_number = index

The output is 4, the index in array a of the last integer. If you want to include types other than integers, you can change the type(2) to type(2.2) or something.

0

To be absolutely sure you find the last instance of "1" you have to look at all the items in the list. There is a possibility that the last "1" will not be the last item in the list. So, you have to look through the list, and remember the index of the last one found, then you can use this index.

list_a = [2, 1, 3, 4, 1, 5, 6]

lastIndex = 0

for index, item in enumerate(list_a):
    if item == 1:
        lastIndex = index

print lastIndex

output:

4

Not the answer you're looking for? Browse other questions tagged or ask your own question.