rsq1725
rsq1725

Reputation: 75

Python loop to combine two lists

Having trouble with concatenating two lists into another list. Where each list item from one list is combined with every list item of the other list.

Example:

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three'] 

I am able to get the desired output in a print statement, using a for loop:

for x in list_1:
    for y in list_2:
        print(x,y)

output:
a one
a two
a three
b one
b two
b three
c one
c two
c three

However, I'm having trouble figuring out how to assign this output to a new list

Upvotes: 1

Views: 238

Answers (9)

Erling Olsen
Erling Olsen

Reputation: 740

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three']

#Initializing an empty list, then inserting it into the empty list
list_3 = []

#Loop
for x in list_1:
    for y in list_2:
        list_3 += [x,y]

#Print list_3
print(list_3)

Upvotes: 0

rsq1725
rsq1725

Reputation: 75

A few of the answers worked to combine the lists but the following usage of itertools provided an output that was easiest to transfer into a dataframe column

from itertools import product
list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three']
list_3 = ['you', 'and', 'me']

result = [f'{a} {b} {c}' for a, b, c in product(list_1, list_2, list_3)]

Output:

['a one you',
 'a one and',
 'a one me',
 'a two you',
 'a two and',
 'a two me',
 'a three you',
 'a three and',
 'a three me',
 'b one you',
 'b one and',
 'b one me',
 'b two you',
 'b two and',
 'b two me',
 'b three you',
 'b three and',
 'b three me',
 'c one you',
 'c one and',
 'c one me',
 'c two you',
 'c two and',
 'c two me',
 'c three you',
 'c three and',
 'c three me']

Upvotes: 0

Amir saleem
Amir saleem

Reputation: 1496

>>> [(i, inner_i )for i in ['a','b','c'] for inner_i in  ['one', 'two', 'three']]

[('a', 'one'),
 ('a', 'two'),
 ('a', 'three'),
 ('b', 'one'),
 ('b', 'two'),
 ('b', 'three'),
 ('c', 'one'),
 ('c', 'two'),
 ('c', 'three')]

Upvotes: 1

Eric
Eric

Reputation: 504

Or you can use the itertools library and make it a one-liner.

import itertools

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three'] 

list_3 = itertools.product(list_1, list_2)

print(list(list_3))

Upvotes: 1

Shashikamal R C
Shashikamal R C

Reputation: 522

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three'] 
list_3 = [[a, b] for a in list_1 for b in list_2]

print(list_3)

output :

[['a', 'one'], ['a', 'two'], ['a', 'three'], ['b', 'one'], ['b', 'two'], ['b', 'three'], ['c', 'one'], ['c', 'two'], ['c', 'three']]

Upvotes: 0

Neal Titus Thomas
Neal Titus Thomas

Reputation: 483

Make a new list (list_3) and append values to it as tuples.

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three']
list_3 = []

for i, in list_1: 
    for j in list_2:
        print(i,j)
        k = i,j
        list_3.append(k)

Upvotes: 0

Ron Thomas
Ron Thomas

Reputation: 837

A list comprehension can also be used.

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three'] 

print([(a, b) for a in list_1 for b in list_2])

Output:

[('a', 'one'), ('a', 'two'), ('a', 'three'), ('b', 'one'), ('b', 'two'), ('b', 'three'), ('c', 'one'), ('c', 'two'), ('c', 'three')]

Upvotes: 3

Krishay R.
Krishay R.

Reputation: 2814

Just create a blank list, and append the values to it after each iteration:

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three']
list_3 = []
for x in list_1:
    for y in list_2:
        # In 1 line: list_3.extend([x, y]) 
        list_3.append(x)
        list_3.append(y)

print(list_3)

Output:

['a', 'one', 'a', 'two', 'a', 'three', 'b', 'one', 'b', 'two', 'b', 'three', 'c', 'one', 'c', 'two', 'c', 'three']

Upvotes: 1

DaneOH-89
DaneOH-89

Reputation: 50

This should do what you want:

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three']

list_3 = []
for x in list_1:
    for y in list_2:
        list_3 += [x,y]

print(list_3)

Upvotes: 1

Related Questions