Vatsal
Vatsal

Reputation: 13

Moving through a list items one by one in python

Hey I am pretty new to python and I suppose this could be a very basic question. I searched through a forum but I couldn't find a matching question as of mine.

I have a list of strings:

target = ('off', 'off', 'off', 'on', 'off', 'on', 'off', 'off', 'off', 'on', 'on', 'off', 'off', 'on', 'off', 'off', 'on', 'off', 'off', 'on')

Now I want to move through the items of the list one by one every time I run the code:

for current_target in target:
print(current_target)

However, the for loop runs through all the items in one go. What I want is that every time I run the code it should move to the next item in the list.

Can anyone please help me solve this?

Thanks Vatsal

EDIT: The OP explanation of the problem in the comments is :

"For instance I wrote a code that could will print a single item from of a list. When I again run that code it'll print the next item. So first it should print the item with index 0 then when I re-run the code it should print the item with the index 1 thus so on and so forth till the end of the list."

Please read this before answering.

Upvotes: 0

Views: 2426

Answers (4)

Raju Pitta
Raju Pitta

Reputation: 606

Create a text file to store the reference of execution. Start with 0 as the content and increment for each execution.Once the list is exhausted reset the content to 0

target = ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'tweleve')

with open("1.txt","r") as f:
    num = int(f.read())
    print target[num]

with open("1.txt","w") as f:
    if num >= len(target)-1:
        f.write("0")
    else:
        f.write(str(num+1))

file:

-bash-4.1$ cat 1.txt
0

Upvotes: 0

VPfB
VPfB

Reputation: 17332

Very basic example of persistent data.

Using a text editor, create a file named e.g. 'list.json' with the desired list of strings:

["off", "off", "off", "on", "off", "on", "off", "off", "off", "on", "on", "off", "off", "on", "off", "off", "on", "off", "off", "on"]

Then every time you run this program, it reads the whole list from the file, prints the first element and saves the rest back to the file. After repeated invocations the list will be consumed and a new one should be prepared.

import json
FILE='list.json'    # should be full path 

with open(FILE) as f:
    lst = json.load(f)
if lst:
    print(lst[0])
    with open(FILE,"w") as f:
        json.dump(lst[1:], f)

Upvotes: 1

mementum
mementum

Reputation: 3203

Are you looking for generators?

For example:

target = ('off', 'off', 'off', 'on', 'off', 'on', 'off', 'off', 'off', 'on', 'on', 'off', 'off', 'on', 'off', 'off', 'on', 'off', 'off', 'on')

def nextitem(items):
    yield from items

loopover = nextitem(target)

while True:
    try:
        print(next(loopover))
    except StopIteration:
        print('reached end of list')
        break

which prints ...

off
off
off
on
off
on
off
off
off
on
on
off
off
on
off
off
on
off
off
on
reached end of list

The generator is freezing the status and each invocation with next gives you the next value. You can keep the generator stored in a data structure, run some other code and recall the generator and use the next value.

With a list you can obviously do directly the same by using loopover = iter(target) directly.

Upvotes: 0

Dan
Dan

Reputation: 671

You can try using pop() to remove items from a copy of the array one by one (preserving the original array in case you need it later). This only works in memory as noted in the comments.

copy_target = array(target)
a = copy_target.pop(0) # Returns 'off'
b = copy_target.pop(0) # Returns the next 'off'

Upvotes: 1

Related Questions