Which of the three do you think is the best coding style or more readable? foo should be running on elements from both dictionaries, but mydict2 may be None
Option 1:
for a,b in mydict1.items(): foo(a,b) if mydict2: for a,b in mydict2.items(): foo(a,b)
Option 2:
for a,b in mydict1.items(): foo(a,b) for a,b in mydict2.items() if mydict2 else dict().items(): foo(a,b)
Option 3:
for a,b in chain(mydict1.items(), mydict2.items() if mydict2 else dict().items()): foo(a,b)
source share