Bottom Page

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)
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  'int' object does not support item assignment shane1236 5 209 Aug-13-2019, 01:53 PM
Last Post: buran
  Checking for an item in a list (if then statement) Sailnir 1 87 Jul-18-2019, 05:36 PM
Last Post: ndc85430
  Error: int object does not support item assignment ankita_nthu 2 184 Jul-07-2019, 02:14 PM
Last Post: ankita_nthu
  Store a product/item in a inventory program viktoria_linn 1 143 Jul-02-2019, 09:26 PM
Last Post: DeaD_EyE
  How define iteration interval increment SriMekala 5 388 Jun-01-2019, 01:06 PM
Last Post: ichabod801
  Sort sets by item values Sergey 4 290 Apr-19-2019, 10:50 AM
Last Post: Sergey
  looking for a multi item container Skaperen 2 218 Apr-15-2019, 04:06 AM
Last Post: Skaperen
  looking fo an expression that yields a dictionary without an item Skaperen 5 357 Apr-09-2019, 02:05 AM
Last Post: Skaperen
  How do I use this? TypeError: 'NoneType' object does not support item assignment ejected 9 1,873 Mar-26-2019, 05:06 AM
Last Post: ejected
  curious syntax with dictionary item inselbuch 3 323 Mar-09-2019, 04:21 PM
Last Post: ichabod801

Forum Jump:

Users browsing this thread: 1 Guest(s)