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
#8
(Jul-07-2021, 12:19 AM)Skaperen Wrote: are you assuming that i need to do an additional action after the last? i need to change the 2nd argument on the last one (even if it is the only one).

As no code provided then it is hard to do anything else but assume. The idea was just very simple beginner level approach:

>>> list_ = []
>>> for i in range(4):
...     list_.append(i**2)   # do something with every item
... else:                    # no-break
...     list_[-1] = i        # redo last one with something else
...
>>> list_
[0, 1, 4, 3]
If operations are not expensive, why not :-)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
checking for last item in for loop - by Skaperen - Jul-04-2021, 07:58 PM
RE: checking for last item in for loop - by Yoriz - Jul-04-2021, 08:42 PM
RE: checking for last item in for loop - by perfringo - Jul-07-2021, 08:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove an item from a list contained in another item in python CompleteNewb 19 6,010 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Iterating over a dictionary in a for loop - checking code has worked sallyjc81 1 2,002 Dec-29-2020, 05:14 PM
Last Post: ndc85430
  Checking for an item in a list (if then statement) Sailnir 1 2,120 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