+ 3

How to exit from a double loop in a pythonic way?

yellow = ['255', '255', '0'] for row in range(50): for col in range(50): pixel = data[row][col] if pixel == yellow: upper_left = (row, col) break When pixel is yellow, how should be this loop terminated? Now it continues with the outer loop again which is not desired.

12th Feb 2020, 7:02 PM
Prof. Dr. Zoltán Vass
27 odpowiedzi
+ 8
for row in range(50): for col in range(50): pixel = data[row][col] if pixel == yellow: upper_left = (row, col) break else: continue break if second loop breaks then first loop breaks too
12th Feb 2020, 7:25 PM
Gabriel Ilie
Gabriel Ilie - avatar
+ 4
The use of itertools.product or a function with return to terminate both loops that you already mentioned seem nice and concise, if that is what Pythonic means.
14th Feb 2020, 12:52 AM
Sonic
Sonic - avatar
+ 4
On another note, would it be better (or more Pythonic) to use a tuple rather than a list to represent each colour, as the number of colour components is always constant(3) and doesn't grow?
14th Feb 2020, 12:54 AM
Sonic
Sonic - avatar
+ 3
I would have also used a control flag(boolean variable) to terminate the outer loop but I guess it's not that Pythonic.
14th Feb 2020, 12:48 AM
Sonic
Sonic - avatar
+ 3
guillem ardanuy flag = False for i in range(8): for j in range(8): if whatever: flag = True break if flag: break This is what I called C-ish. If I knew a shorter, *easily readable* technique, I would choose it. Maybe it's also a bit a matter of getting used to the pattern else break
14th Feb 2020, 7:36 PM
HonFu
HonFu - avatar
+ 2
Gabriel Ilie Another solution, seems to me more pythonic: def find_yellow(): for raw in range(50): for col in range(50): if data[raw][col] == yellow: return (raw, col) upper_left = find_yellow()
12th Feb 2020, 8:30 PM
Prof. Dr. Zoltán Vass
+ 2
guillem ardanuy Many people use a boolean flag as control variable but the code is longer, eg: found = False while not found: for row in range(50): for col in range(50): pixel = data[row][col] if pixel == yellow: upper_left = (row, col) found = True break if found: break
13th Feb 2020, 4:53 AM
Prof. Dr. Zoltán Vass
+ 2
guillem ardanuy Mirielle👽 For me, this 4-liner seems to be the most pythonic: from itertools import product for row, col in product(range(50), range(50)): if data[row][col] == ['255', '255', '0']: break
13th Feb 2020, 5:02 AM
Prof. Dr. Zoltán Vass
+ 2
Mirielle👽 Thanks for your comment and sorry for my unclear or uncomplete question. The code actually works because the list is a matrix of strings, eg. [[[‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’]...]]. In this way, list items can be matched the variable yellow, eg: data[25][25] # it is ['255', '255', '0'] yellow = ['255', '255', '0'] so data[25][25] == yellow is True
13th Feb 2020, 8:19 AM
Prof. Dr. Zoltán Vass
+ 2
Sonic “Pythonic” is somewhat subjective but there are guidelines, ie. The Zen of Python or PEP8. “Exploiting the features of the Python language to produce code that is clear, concise and maintainable. Pythonic means code that doesn't just get the syntax right but that follows the conventions of the Python community and uses the language in the way it is intended to be used.” https://stackoverflow.com/questions/25011078/what-does-pythonic-mean
14th Feb 2020, 5:28 AM
Prof. Dr. Zoltán Vass
+ 2
HonFu You are very advanced. Which solution would you prefer for exiting double loops?
14th Feb 2020, 6:35 PM
Prof. Dr. Zoltán Vass
+ 2
guillem ardanuy Just a quick tipp: you can try to use the @ sign before usernames.
14th Feb 2020, 7:13 PM
Prof. Dr. Zoltán Vass
+ 1
Mirielle👽 Thanks, Mirielle. How to break the first loop (row) when yellow is found?
12th Feb 2020, 7:22 PM
Prof. Dr. Zoltán Vass
+ 1
Mirielle👽 Actually, this list is a matrix of strings, eg. [[[‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’]...]] which represents an 50x50 image’s RGB pixels
12th Feb 2020, 7:38 PM
Prof. Dr. Zoltán Vass
+ 1
Gabriel Ilie Yes, this works!
12th Feb 2020, 7:38 PM
Prof. Dr. Zoltán Vass
+ 1
🤣 double loop is double loop🧐🧐 You might hide it through some syntactic sugar... Double loop stays double loop
12th Feb 2020, 7:56 PM
Oma Falk
Oma Falk - avatar
+ 1
from itertools import product for row, col in product(range(50), range(50)): if data[row][col] == ['255', '255', '0']: upper_left = (row, col) break
12th Feb 2020, 8:47 PM
Prof. Dr. Zoltán Vass
+ 1
Its a good idea to put all the code inside a while statement, and when yellow, The control variable equal false? ... Control=true While(control) ... If yellow Control=false .... Good or bad idea?
13th Feb 2020, 12:59 AM
guillem ardanuy
guillem ardanuy - avatar
+ 1
Sonic Yes, that’s right! Tuples are even faster for iteration
14th Feb 2020, 5:11 AM
Prof. Dr. Zoltán Vass
+ 1
Gabriel Ilie, funny hack, never saw that before. :)
14th Feb 2020, 6:30 PM
HonFu
HonFu - avatar