Search code examples
pythonlistexpand

Expand List within List


Is there an easy way in python to expand lista into listb?

lista = [['apples', ['bob','mary'], 2020],
['bananas', ['john', 'bill', 'chris'], 2019]]


listb = [['apples', 'bob', 2020],
['apples', 'mary', 2020],
['bananas', 'john', 2019],
['bananas', 'bill', 2019],
['bananas', 'chris', 2019]]

Solution

  • You just wanna repeat the first and last elements for all the middle elements? This should work:

    listb = []
    for lia in lista:
      for item in lia[1]:
        listb.append([lia[0],item,lia[2]])