Norrven
Norrven

Reputation: 23

To each element (string) of the list1 add an element from the list2 in Python

There are two lists:

list1  = ["shirts","jeans"]

list2 = ["white","red","yellow"]

From these two lists I want to make the third one:

list3 = ["white shirts","red shirts","yellow shirts","white jeans","red jeans", "yellow jeans"]

So that for each element (string) of list 1 to be added an element from the list 2.

I've got the following code:

my_list1 = ['shirts', 'jeans']
string = ' red'
my_new_list1 = [x + string for x in my_list1]
print (my_new_list1)

my_list2 = ['shirts', 'jeans']
string = ' white'
my_new_list2 = [x + string for x in my_list2]
print (my_new_list2)

my_list3 = ['shirts', 'jeans']
string = ' yellow'
my_new_list3 = [x + string for x in my_list3]
print (my_new_list3)

but this piece of code isn't quite exactly whats needed. moreover, its needed that list 1 & list 2 were taken from list1.txt & list2.txt, and the resulting list 3 to be exported to list3.txt.

Upvotes: 2

Views: 157

Answers (6)

Diana
Diana

Reputation: 140

I'm guessing you're trying to do something like this:

l1 = ['shirts', 'jeans']
l2 = ['white', 'red', 'yellow']
l3 = [j + " " + i for i in l1 for j in l2]

print(l3)

So the output will be:

['white shirts', 'red shirts', 'yellow shirts', 'white jeans','red jeans', 'yellow jeans']

Edited:

So, with .txt files I would do this:

import pandas as pd

l1 = pd.read_csv('list1.txt', header = None).values.flatten()
l2 = pd.read_csv('list2.txt', header = None).values.flatten()
l3 = [j + " " + i for i in l1 for j in l2]

with open('list3.txt', 'w') as f:
    for item in l3:
        f.write("%s\n" % item)

Upvotes: 1

Joao Pedro Martins
Joao Pedro Martins

Reputation: 53

I think you were not as clear as expected, so the solution i'll post here might be incorrect. For your algorithm to work with the files you've mentioned, the following code should do:

with open('list1.txt') as f:
    list1 = f.read().splitlines()
with open('list2.txt') as f:
    list2 = f.read().splitlines()
# my_list1 = ['shirts', 'jeans']
list3 = []
for i in range(len(list2)):
    list3.append(list2[i] +' '+ list1[i])
print(list3)

Upvotes: 1

Daniel
Daniel

Reputation: 5381

You can do a list comprehension to get list3

list3 =   ["{} {}".format(y,x) for x in list1 for y in list2]

output

     ['white shirts',
 'red shirts',
 'yellow shirts',
 'white jeans',
 'red jeans',
 'yellow jeans']

Upvotes: 1

py_dude
py_dude

Reputation: 832

This will do the job:

list3 = [y + " " + x for y in list1 for x in list2]

Should result be sorted like that: list3 = ["white shirts","red shirts","yellow shirts","white jeans","red jeans", "yellow jeans"]?

Upvotes: 1

sahasrara62
sahasrara62

Reputation: 11228

# your code goes here
list1 = ['shirts','jeans']
list2 = ['white','red','yellow']
result = ['{} {}'.format(color, cloth) for cloth in list1 for color in list2 ]
print(result)

output

['white shirts', 'red shirts', 'yellow shirts', 'white jeans', 'red jeans', 'yellow jeans']

Upvotes: 1

Horace
Horace

Reputation: 1054

If the order is not important, you can do:

from itertools import product 

l1 = ["shirts", "jeans"]
l2 = ["white", "red", "yellow"]

result = [' '.join(a) for a in product(l2, l1)]
['white shirts',
 'white jeans',
 'red shirts',
 'red jeans',
 'yellow shirts',
 'yellow jeans']

Upvotes: 3

Related Questions