Looking for assistance from the experts here to help make good choices in a program that I am creating. Which of the two approaches for creating a list appears more Pythonic and readable to you? Or is there a better way that I could be doing this?
Approach #1 - list comprehension
def test_func(*args):
s = 'Country name: United {nm}'
l = [s.format(nm='States') if x is 'us'
else s.format(nm='Arab Emirates') if x is 'uae'
else s.format(nm='Kingdom') if x is 'uk'
else 'Unknown' for x in args]
return l
# execute
test_func('us', 'uk', 'uae')
# results
['Country name: United States',
'Country name: United Kingdom',
'Country name: United Arab Emirates']
Approach #2 - for loop
def test_func(*args):
s = 'Country name: United {nm}'
l = []
for arg in args:
if arg is 'us':
l.append(s.format(nm='States'))
elif arg is 'uk':
l.append(s.format(nm='Kingdom'))
elif arg is 'uae':
l.append(s.format(nm='Arab Emirates'))
else:
l.append(s.format(nm='Unknown'))
return l
# execute
test_func('us', 'uk', 'uae')
# results
['Country name: United States',
'Country name: United Kingdom',
'Country name: United Arab Emirates']