+ 2
Using else alongside for loop means that, the code in the else should run after normal execution of the for loop
+ 2
I woud create a variable outside of the for loop with the default value "Not Found", inside the same as you use in print "{c[0]} is {c[1]}" and finally print the variable auside the for loop
+ 1
But in the above case, there is break hence the else might not run since we are breaking out
+ 1
Yarik Yaroo
For-else loop is a thing in Python.
There is a while-else, too.
But yes, they are rarely used. Probably because we can do without them. But after reading about it, there are some merit in using them.
https://book.pythontips.com/en/latest/for_-_else.html
https://www.pythontutorial.net/python-basics/python-for-else/
+ 1
Yarik Yaroo
alternatively, you can create a dict from the list of paired tuples.
c_dict = dict((k,v) for k,v in contacts)
if name in c_dict:
print(f"{name} is {c[name]}")
else :
print("Not found")