【Python】for-else文でループ脱出後の処理

for文で、ループから脱出する&別の処理を実装、を実現するなら、for-else文がおすすめ。

 

for文内で実行できるので、for文とは別に実装するよりも手間がかからない。

 

dictionary = {'one':1,'second':2, 'third':3}
for key, value in dictionary.items():
    print(key, value)
else:
    print('End')

"""
one 1
second 2
third 3
End
"""