Search code examples
pythonlist-comprehension

Creating one list by combining two list in a list comprehension


new to Python. Tried to find an answer but couldn't find anything or understand anything. I'm ok with for loops but I'm trying to combine two lists into one through a list comprehension. I'm fairly new to list comprehensions. So Something like:

list1 = ['a','b','c']
list2 = ['p','q','r','s','t','u']

I want an output that comes out like this:

list3 = ['ap','aq','ar','as','at','au','bp','bq','br','bs','bt','bu','cp','cq','cr','cs','ct','cu'...]

I didn't write the whole thing but I hope it gets the point across. How can I do this based solely on a list comprehension?


Solution

  • You can try

    list1 = ['a','b','c']
    list2 = ['p','q','r','s','t','u']
    
    result = [ a+b for a in list1 for b in list2 ]