Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for loop with 2 conditions
#1
Hello,

I'm testing to use a for loop that stops by 2 conditions. For example:

l1 = ['hello', 'bye', 'now', 'before', 'after']
count = 3
for word in l1:
	print(word)
But I want to stop not only when l1 is finished, also when count is 0, and I do it:
l1 = ['hello', 'bye', 'now', 'before', 'after']
count = 3
for word in l1:
	count -=1
	print(word)
	if count == 0:
		break
I don't like... I think that Python should provide me with a more elegant way to do it.
Is it possible?

Thanks
Reply
#2
If your intention is to go through first three list items you can do it like this:

l1 = ['hello', 'bye', 'now', 'before', 'after']

for word in l1[0:3]:
    print(word)
Reply
#3
Hello ODIS,

thank you. It works.

My English level is low and for this, I simplified the problem, but really I don't have a list (l1), I've a buffer that I don't know how many elements it has, and it changes dynamically.

Then, if use [0:count], maybe I will be out of range.

Of what I'm sure, is that wen I access an element,it exists, because buffer fills up faster than I can empty it.

I hope that I have explained myself acceptably.

Thanks a lot.
Reply
#4
Can you please put here more of you code for better understanding its functionality?
Reply
#5
Ok ODIS, I try it.. ;) Remember my low English level if I'm wrong whith any word...

First of all, thank you.

I've a buffer that reads from a queue (a kafkaConsumer, but it is not relevant). Then I don't know how many items has 'l1'. Now 'l1' has 3 items, 5 seconds later it has 15 items... But there is always at least one element. Once a value is readed, it is deleted from the buffer.

In each iteration, I read the first value and I work with it. Then If I do this, all works:
#Really, I do not know what is in 'buf'
buf = ['hello', 'bye', 'now', 'before', 'after']
count = 3
for element in buf:
    print(element)
But the buffer never will be empty, and then, the for is running forever. For avoid this, I want to define a number of iterations to stop de loop. I want to do that if I'm in the N iteration, stop to read from the buffer.

I get it:
#Really, I do not know what is in 'buf'
buf = ['hello', 'bye', 'now', 'before', 'after']
count = 3
for element in buf:
    count -=1
    print(element)
    if count == 0:
        break
But I think that is not a god solution. Not elegant. I think that Python must provides me other better way.

In simple list loops, I can use 'if' statements to control this. It is 'pretty'. For example:

test = [i for i in range(200) if i < 10]
print (test)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In this simple example, althoung the 'for' loop ends in 200 iterations, the 'if' statement makes that really the loop ends at 10 iterations. But I can't use a list for my problem, because in each iteration I work a lot with the item that I read in the bucle. I need a structure like:

#Really, I do not know what is in 'buf'
buf = ['hello', 'bye', 'now', 'before', 'after']
count = 3
for element in buf: #if count >0, 'for example'
    print(element)
I hope to I have explained myself good. ;) Thanks
Reply
#6
You could write a generator that makes it look nicer.

>>> def top(count, items):
...   items = iter(items)
...   while count > 0:
...     yield next(items)
...     count -= 1
...
>>> buf = ['hello', 'bye', 'now', 'before', 'after']
>>> for element in top(3, buf):
...   print(element)
...
hello
bye
now
On the other hand, something like that is already available in itertools:
>>> import itertools
>>> for element in itertools.islice(buf, 3):
...   print(element)
...
hello
bye
now
Reply
#7
Actually the list slicing is not trowing any exception, so you can use it also in situation when you have less items in a list (at least in Python 3):

l1 = ['hello']
 
for word in l1[0:3]:
    print(word)
If you want to iterate through all items that you have in a list at the moment, you can make a slice of the current length like this:

l1 = ['hello', 'bye', 'now', 'before', 'after']

for word in l1[0:len(l1)]:
    print(word)
So the out of range can never happen.
Reply
#8
...as long as it's a list. If it's a different sort of iterable, such as a file or stream/buffer, you can't slice it.
Reply
#9
You're right. It was presented as a list type, so I went with that :)
Reply
#10
Hi,

thank you. I will work with this. When I have it done, I will write it here.

;)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does while-If-elif-else-If loop conditions check run mrhopeedu 2 1,772 Oct-27-2019, 04:56 AM
Last Post: mrhopeedu
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,943 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  applying 2 conditions to a loop Pedroski55 1 2,835 Nov-08-2017, 07:11 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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