I believe the most "pythonic" way would be to use itertools.chain
:
list_a =['hii','hello','hai']
list_b = ['john','david']
for name in itertools.chain(list_a, list_b):
print(name)
The cool thing about this is that it works for any iterables, not just list
s. So you could combine lists, sets, an iterable of dictionary keys, a custom generator function, etc. all into one iterable.
You can also pass a list of iterables instead of variadic arguments by using itertools.chain.from_iterable
:
list_a = ['hii', 'hello', 'hai']
list_b = ['john', 'david']
main_list = [list_a, list_b]
for name in itertools.chain.from_iterable(main_list):
print(name)
And if you really need the elements of main_list
to be strings (protip: you probably don't):
main_list = ['list_a', 'list_b']
for name in itertools.chain.from_iterable(globals().get(list_name, []) for list_name in main_list):
...