Python Forum
checking for last item in for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
checking for last item in for loop
#11
Grab the last and use it for comparison
the_list = [1,2,3,4]
final = the_list[-1] 
for i in the_list:
    if i == final:
        print('Processing last')
    else:
        print(f"Processing {i}")
Output:
Processing 1 Processing 2 Processing 3 Processing last
Reply
#12
what if the code that needs to be slightly changed is a function call with one of many arguments changed?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove an item from a list contained in another item in python CompleteNewb 19 5,549 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Iterating over a dictionary in a for loop - checking code has worked sallyjc81 1 1,882 Dec-29-2020, 05:14 PM
Last Post: ndc85430
  Checking for an item in a list (if then statement) Sailnir 1 2,014 Jul-18-2019, 05:36 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020