nvergos nvergos - 1 year ago 82
Python Question

Access all elements of nested lists

I am trying to find a short and elegant way in order to access all individual elements in nested lists. For example:

lst1 = ['1', '2', '3']
lst2 = ['4', '5']

outer = [lst1, lst2]

Is there a list comprehension that would return
['1', '2', '3', '4', '5']

Answer Source

There are two short similar ways to do it:

import itertools

# with unpacking

# without unpacking
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download