3

How do we skip to the last element in python?

ITS CLEAR NOW!!!!!!!!!!!!

So for example i have a list

foo = ['hello', 'world','how']

And, I have this loop

for i in foo:
    ----------Codes to skip to last element.--------

therefore, variable 'i' becomes the last element in foo, which is 'how'.

    if i == 'how':
       print 'success!'

And yes, i was wondering if there is any method to do so in a for loop, not outside.

The closest I have found is this:

Python skipping last element in while iterating a list using for loop

Please add in the explanation for your answers on how it works.

Edit: Also, if it's not possible, please explain why(optional).

2
  • 1
    if you what to skip to the last element, then why you use a for loop? why not directyly do something with foo[-1] Commented Dec 25, 2013 at 11:36
  • @Ever I just want to learn even more methods!!! Commented Dec 25, 2013 at 12:06

4 Answers 4

6

You don't have to run on the entire list:

foo = ['hello', 'world','how']

for i in foo[:-1]:
    ........

You can also identify if it's the last by doing:

foo = ['hello', 'world','how']

for i in foo:
    if i == foo[-1]:
        print "I'm the last.."

For more information about list you can refer Docs

And you have a very good tutorial here

Sign up to request clarification or add additional context in comments.

4 Comments

No, but is it possible in the for-loop?
I mean inside the loop, not on the loop, replacing my commented lines in the code given in the question.
I want the variable i to skip to the last character IN THE LOOP.
@leonneo there are no characters since you are iterating items of a list! please try to edit your question and give some example so it will be easy to understand.
2

Another method to skip the last element

>>> for index, item in enumerate(foo):
        if index==(len(foo)-1):
            # last element 
        else:
            # Do something

2 Comments

Why enumerate? Can you explain?
enumerate will make certain loops a bit clearer. you will get index of the item along with the item in the sequence.As you need to skip the last element I have taken index and compared it with the len of list to skip the last
1

My understanding is that you want to check whether you've reached the end of the cycle without break ing out of it. Python has beautiful for...else and while..else constructs for that. The else clause is used when the cycle completes without breaking out So that the skeleton for your code:

for word in foo:
   ... you may break out
else:
    print success

Simple and elegant (and more efficient too)

EDIT: OK, now I get what you want. You may use slicing - as it was suggested, but islice of itertools will do the job better:

from itertools import islice:
for word in islice(foo, len(foo)-1):

3 Comments

What breakout? What do you mean?
In a loop, you may put code if something: break . The meaning is that if condition something evaluates to true, your loop will be interrupted immediately.
That's for everyone to refer to only. I am making sure they can add all they want. Also, you can help edit, because it is unclear from the comments
0

You can skip any statement either in last or middle.

foo = ['hello', 'world','how']

for i in foo:
    if i == "how" or i == "hello":
        continue
    print(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.