If you want to find the first number that matches some criteria, what do you do? The easiest way is to write a loop that checks numbers one by one and returns when it finds the correct one.

Let’s say we want to get the first number divided by 42 and 43 (that’s 1806). If we don’t have a predefined set of elements (in this case, we want to check all the numbers starting from 1), we might use a “while loop”.

## find_item.py

	def while_loop():
	    item = 1
	    ## You don't need to use parentheses, but they improve readability
	    while True:
	        if (item % 42 == 0) and (item % 43 == 0):
	            return item
	        item += 1

#python #writing-faster-python #tips-and-tricks #best-practices #performance

Python: The Fastest Way to Find an Item in a List
12.60 GEEK