Python Forum
How can I increment a List item with in a "for in"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I increment a List item with in a "for in"
This is likely Python 101.

I have searched for a straight forward simple answer to no avail.
How can I increment a list item while inside a "for x in List" loop.
I want to process two list items in sequence based on the first item having a given value. Then move on through the loop structure.

items = [ "Thing", "apple", "dog", "house", "Thing", "bugs", "ducks", "rabbits"]
#  Start my loop over the list
for i in items:    
	if i == "Thing":   
		# increment(i)??
		print(i) # this should print the next item in the list after the last "Thing"
# output I want is:
# Thing 
# apple
# Thing
# bugs
The simple way would be just to keep track of the last item:

is_thing = False
for item in items:
    if is_thing:
    is_thing = item == 'Thing'
Another way is to zip the list to the list without the first item:

for first, second in zip(items, items[1:]):
    if first == 'Thing':
If you are unfamiliar with zip, it takes two (or more) sequences and returns tuples of the nth item of each sequence. So zip('123', 'abc') gives you ('1', 'a'), then ('2', 'b'), then ('3', 'c').
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Another way is to turn the list into an iterable so next can be called on items.

items = ["Thing", "apple", "dog", "house", "Thing", "bugs", "ducks", "rabbits"]
#  Start my loop over the list
items = iter(items)
for item in items:
    if item == "Thing":
        # increment(i)??
            # this should print the next item in the list after the last "Thing"
        except StopIteration:
Thing apple Thing bugs
another variation is to use enumerate
items = ["Thing", "apple", "dog", "house", "Thing", "bugs", "ducks", "rabbits"]
for num, item in enumerate(items):
    if item == "Thing":
        except IndexError:
Thing apple Thing bugs
You should be aware that you can get an IndexError if you go outside the range of the list though - Fixed Added try/except - Yoriz
You don't have to use a for loop. Here an example, which works also with tuple.

items = ["Thing", "apple", "dog", "house", "Thing", "bugs", "ducks", "rabbits"]
element = "dog"

    index = items.index(element)
except VaslueError:
    index = -1
    print(element, 'not found')
    print('Index is', index)
    print(element, 'found')
    print(element, 'is at index', index)
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to easily create a list of already existing item CompleteNewb 15 474 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Remove an item from a list contained in another item in python CompleteNewb 19 1,274 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  count item in list korenron 8 1,146 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Character Increment AnokhiRaaz 1 1,047 Apr-22-2021, 04:29 AM
Last Post: buran
  Time.sleep: stop appending item to the list if time is early quest 0 780 Apr-13-2021, 11:44 AM
Last Post: quest
  Increment text files output and limit contains Kaminsky 1 1,263 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  How to run a pytest test for each item in a list arielma 0 938 Jan-06-2021, 10:40 PM
Last Post: arielma
  How do I add a number to every item in a list? john316 2 740 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Ignoring a list item hank4eva 2 978 Aug-17-2020, 08:40 AM
Last Post: perfringo
  Select correct item from list for subprocess command pythonnewbie138 6 1,448 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

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