There is a loop in a loop, if condition is met some code runs. However, if condition is not met, I need it to print something. Though if I add code to any location, it is printed multiple times. How to make it print only once, if condition is not met?
some_list = {'a_list': [{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 25}, {'name': 'Val', 'age': 25}], 'b_list': [{'name': 'Don', 'age': 25}, {'name': 'Tim', 'age': 25}, {'name': 'San', 'age': 25}]}
findperson = 'San'
for i in some_list:
for y in some_list[i]:
if y['name'].lower() == findperson.lower():
print('Friend found')
break
else:
print('Friend not found')
You could use any
for the inner loop (and break
from the outer loop)...
for i in some_list:
if any(y['name'].lower() == findperson.lower() for y in some_list[i]):
print('Friend found')
break
else:
print('Friend not found')
... or even for the whole thing:
if any(y['name'].lower() == findperson.lower()
for i in some_list for y in some_list[i]):
print('Friend found')
else:
print('Friend not found')
If you also need the actual friend, you can use next
:
for i in some_list:
friend = next((y for y in some_list[i] if y['name'].lower() == findperson.lower()), None)
if friend is not None:
print('Friend found:', friend)
break
else:
print('Friend not found')
Also works with nested generator, like with any
above:
friend = next((y for i in some_list for y in some_list[i]
if y['name'].lower() == findperson.lower()),
None)
if friend is not None:
print('Friend found:', friend)
else:
print('Friend not found')
Instead of using flags and break
(which only breaks the inner loop) another possible solution would be to use a function. This way you can simply use return
. This also has the benefit of stopping the search once a match is found.
some_list = {'a_list': [{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 25}, {'name': 'Val', 'age': 25}],
'b_list': [{'name': 'Don', 'age': 25}, {'name': 'Tim', 'age': 25}, {'name': 'San', 'age': 25}]}
search_name = 'San'
def find_person(data, name_to_find):
for i in data:
for y in data[i]:
if y['name'].lower() == name_to_find.lower():
print('Friend found')
return
print('Friend not found')
find_person(some_list, search_name)
Try this:
some_list = {'a_list': [{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 25}, {'name': 'Val', 'age': 25}], 'b_list': [{'name': 'Don', 'age': 25}, {'name': 'Tim', 'age': 25}, {'name': 'San', 'age': 25}]}
findperson = 'San'
found = False
for i in some_list:
for y in some_list[i]:
if y['name'].lower() == findperson.lower():
print('Friend found')
found = True
if found:
break
if not found:
print('Friend not found')
for else
is a good trial with break
condition, but it only works when you get 1-for loop, but here you get 2-for loop, you can use a flag:
some_list = {'a_list': [{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 25}, {'name': 'Val', 'age': 25}], 'b_list': [{'name': 'Don', 'age': 25}, {'name': 'Tim', 'age': 25}, {'name': 'San', 'age': 25}]}
findperson = 'San'
found = False
for i in some_list:
for y in some_list[i]:
if y['name'].lower() == findperson.lower():
print('Friend found')
found = True
break
if found:
break
if not found:
print('Friend not found')
The best option imo is to put the nested for-loops in a function and return when you have found your friend, but if for some reason this isn't possible, you could end your nested for loops prematurely when you have found your friends without using a flag by doing something like
some_list = {'a_list': [{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 25}, {'name': 'Val', 'age': 25}],
'b_list': [{'name': 'Don', 'age': 25}, {'name': 'Tim', 'age': 25}, {'name': 'San', 'age': 25}]}
findperson = 'San'
for i in some_list:
for y in some_list[i]:
if y['name'].lower() == findperson.lower():
print('Friend found')
break
else:
continue
break
else:
print('Friend not found')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.