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


Messages In This Thread
for loop with 2 conditions - by vaison - Apr-14-2018, 02:03 AM
RE: for loop with 2 conditions - by ODIS - Apr-14-2018, 03:03 AM
RE: for loop with 2 conditions - by vaison - Apr-14-2018, 11:32 AM
RE: for loop with 2 conditions - by ODIS - Apr-15-2018, 03:20 PM
RE: for loop with 2 conditions - by vaison - Apr-18-2018, 02:29 PM
RE: for loop with 2 conditions - by nilamo - Apr-18-2018, 03:56 PM
RE: for loop with 2 conditions - by ODIS - Apr-18-2018, 04:04 PM
RE: for loop with 2 conditions - by nilamo - Apr-18-2018, 04:24 PM
RE: for loop with 2 conditions - by ODIS - Apr-18-2018, 04:46 PM
RE: for loop with 2 conditions - by vaison - Apr-19-2018, 01:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How does while-If-elif-else-If loop conditions check run mrhopeedu 2 1,795 Oct-27-2019, 04:56 AM
Last Post: mrhopeedu
  Do break operators turn while loop conditions from True to False? Drone4four 5 3,019 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  applying 2 conditions to a loop Pedroski55 1 2,871 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