Search code examples
python-3.xlistdictionarylist-comprehensionnested-lists

Data Handling with Lists


My problem is related to handling data from multiple lists. Consider I have two lists.

a = ['a','b','c']
b = ['d','e','f','g','h']

I want to create a nested list which will add all the elements of one list (list a) into another (list b) without altering any elements in the second list (list b).

So, the output would look like:

output = [['a','d','e','f','g','h'], ['b','d','e','f','g','h'],['c','d','e','f','g','h']]

Can anyone say an efficient way to achieve this output. Thank you in advance.


Solution

  • This list comprehension seems to do the job:

    output = [list(item) + b for item in a]