Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 667647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:57:06+00:00 2026-05-13T23:57:06+00:00

I need to do some special operation for the last element in a list.

  • 0

I need to do some special operation for the last element in a list.
Is there any better way than this?

array = [1,2,3,4,5] 
for i, val in enumerate(array): 
  if (i+1) == len(array): 
    // Process for the last element 
  else: 
    // Process for the other element 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:57:06+00:00Added an answer on May 13, 2026 at 11:57 pm
    for item in list[:-1]:
        print "Not last: ", item
    print "Last: ", list[-1]
    

    If you don’t want to make a copy of list, you can make a simple generator:

    # itr is short for "iterable" and can be any sequence, iterator, or generator
    
    def notlast(itr):
        itr = iter(itr)  # ensure we have an iterator
        prev = itr.next()
        for item in itr:
            yield prev
            prev = item
    
    # lst is short for "list" and does not shadow the built-in list()
    # 'L' is also commonly used for a random list name
    lst = range(4)
    for x in notlast(lst):
        print "Not last: ", x
    print "Last: ", lst[-1]
    

    Another definition for notlast:

    import itertools
    notlast = lambda lst:itertools.islice(lst, 0, len(lst)-1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some effective way to escape all special characters in string.For example, my
I need a special operator that's maybe a bit better than LIKE to query
I need to set some special style for an element if some other element
I need to escape some special characters from a JS string, but I can
I need to pass '+' via the QueryString . some special character can be
Need some quick advice I am trying to access a object array but I
Need some help to solve this. I have a gridview and inside the gridview
I need to do some special routing in cake, but can't for the life
i need to add video streaming capabilities to drupal. I need this functions: Some
Can you suggest some free software for recording screencasts? I don't need some special

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.