Python Forum
How can I increment a List item with in a "for in" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How can I increment a List item with in a "for in" (/thread-20488.html)



How can I increment a List item with in a "for in" - msteffes - Aug-13-2019

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":   
		print(i) 
		# 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



RE: How can I increment a List item with in a "for in" - ichabod801 - Aug-13-2019

The simple way would be just to keep track of the last item:

is_thing = False
for item in items:
    if is_thing:
        print(item)
    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':
         print(second)
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').


RE: How can I increment a List item with in a "for in" - Yoriz - Aug-13-2019

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":
        print(item)
        # increment(i)??
        try:
            # this should print the next item in the list after the last "Thing"
            print(next(items))
        except StopIteration:
            pass
Output:
Thing apple Thing bugs



RE: How can I increment a List item with in a "for in" - metulburr - Aug-13-2019

another variation is to use enumerate
items = ["Thing", "apple", "dog", "house", "Thing", "bugs", "ducks", "rabbits"]
for num, item in enumerate(items):
    if item == "Thing":
        print(item)
        try:
            print(items[num+1])
        except IndexError:
            pass
Output:
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


RE: How can I increment a List item with in a "for in" - DeaD_EyE - Aug-14-2019

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"

try:
    index = items.index(element)
except VaslueError:
    index = -1
    print(element, 'not found')
    print('Index is', index)
else:
    print(element, 'found')
    print(element, 'is at index', index)