How to Concatenate Element-Wise Two Lists in Python

How to concatenate element-wise two lists in Python

Use zip:

>>> ["{}{:02}".format(b_, a_) for a_, b_ in zip(a, b)]
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']

How to concatenate element-wise two lists of different sizes in Python?

[x + y for x in list_1 for y in [""] + list_2]

produces output:

['url1.com/',
'url1.com/route1',
'url1.com/route2',
'url2.com/',
'url2.com/route1',
'url2.com/route2',
'url3.com/',
'url3.com/route1',
'url3.com/route2']

BTW, the term you're looking for is Cartesian Product (with a slight modification) rather than elementwise concatenation, since you're going for each possible combination.

How to combine two lists of lists element-wise?

Since you are having nested lists, you need to double zip through your list. You can do:

[[a + b for a, b in zip(x, y)] for x, y in zip(firstList, secondList)]

Code:

firstList = [[[1], [2], [3]], [[4], [5], [6]]]
secondList = [[[11], [12], [13]], [[14], [15], [16]]]

result = [[a + b for a, b in zip(x, y)] for x, y in zip(firstList, secondList)]
# [[[1, 11], [2, 12], [3, 13]], [[4, 14], [5, 15], [6, 16]]]

How to concatenate two lists element - wise and make a new list?

[' - '.join(x) for x in zip(quotes, authors)] zip lets you loop over elements of both lists at once, and ' - '.join() combines the elements separated by a dash

Element wise concatenate multiple lists (list of list of strings)

Here's one way zipping the sublists and mapping with ''.join the resulting tuples:

list(map(''.join, zip(*lst)))
# ['a@1', 'b$2', 'c#3']

Here zip as shown in the docs aggregates elements from several iterables. With *, we are unpacking the list into separate iterables, which means that the function will instead be receiving zip(['a','b','c'],['@','$','#'],['1','2','3']).

Now at each iteration, map will be applying ''.join to each of the aggregated iterables, i.e to the first element in each sublist, then the second, and so on.

Concatenating two lists of Strings element wise in Python without Nested for loops

The technique you have used is perfectly Pythonic, and until list comprehensions were introduced into the language would have been canonical. The one you suggest using zip, however, won't work, because you want all pairs of elements from ls1 and ls2, but zip simply creates pairs using the corresponding elements rather than all combinations.

If you'd like to use more compact code then the appropriate list comprehension would be

ls3 = [x+'-'+y for x in ls1 for y in ls2]

For large lists, or where you need every ounce of performance (which should never be your first consideration) see the answer from @PaulPanzer, who explains a more efficient though slightly more complex technique.

How to append element-wise of two lists in Python, where at least one of them is string

Try this :

[k for i in zip(map(str, a), b) for k in i]

Output :

['1', '&', '2', '&', '3', '\\']

One limitation of using zip here would be it would iterate up to length of the smallest list, either a or b. For cases like the following :

a = [1, 2, 3, 4]
b = ['&','&','\\']

the above code would produce same result ['1', '&', '2', '&', '3', '\\'] and won't bother to go beyond 3 elements. To circumvent this, you can use zip_longest.

How do I concatenate two lists in Python?

Use the + operator to combine the lists:

listone = [1, 2, 3]
listtwo = [4, 5, 6]

joinedlist = listone + listtwo

Output:

>>> joinedlist
[1, 2, 3, 4, 5, 6]


Related Topics



Leave a reply



Submit