[IMPR] Avoid deeply nested flow statements
[IMPR] Avoid deeply nested flow statements

- for loop is able to handle empty iterators
- use a tuple with endswith to reduce one for loop

Change-Id: I47c39266b26f6492faa8cc4a9c66a785808224a9

PersianWikipedia/pywikibot-core,wikimedia/pywikibot-core,wikimedia/pywikibot-core