In python, how do I concatenate 3 lists using a list comprehension?
Have:
list1 = [1,2,3,4]
list2 = [5,6,7,8]
list3 = [9,10,11,12]
Want:
allList = [1,2,3,4,5,6,7,8,9,10,11,12]
I tried using a list comprehension, but I'm not very good at them yet. These are what I have tried:
allList = [n for n in list1 for n in list2 for n in list3 ]
this was a bad idea, obviously and yielded len(list1)*len(list2)*len(list3) worth of values. Oops. So I tried this:
allList = [n for n in list1, list2, list3]
but that gave me allList = [list1, list 2, list3] (3 lists of lists)
I know you can concatenate using the + operator (as in x = list1 + list2 + list3)but how do you do this using a simple list comprehension?
There is a similar question here: Concatenate 3 lists of words , but that's for C#.