Combining lists or joining elements in Python: explanation and examples
This article covers the following topics about combining lists in Python.
- Combining list elements with the
join()
function (to string, numbers) - Using with separator in the
join()
function (quotes"
,'
, comma,
, space - Combining lists into one
- Merging lists together without duplicates
- Combining by list index (using
map()
)
Primarily, we'll look at how to combine elements within a list and how to combine multiple lists themselves.
See this post for how to add new elements to a list.
1. Combining elements of list to string with join()
In Python, the join
method provides the ability to join elements of a list using strings.
Specifically, it joins each element of the list into a single string, separated by the specified delimiter.
To illustrate with a simple example, suppose we have the following list:
fruits = ['apple', 'banana', 'orange']
We want to combine the elements of this list into a single string, separated by commas. To do this, we can use the join()
method:
result = ', '.join(fruits)
print(result)
# Output: apple, banana, orange
Here, ', '
are used as delimiters, and the join()
method joins the elements of the list fruits
into a single string, separated by commas and spaces.
Note that to use the join
method, all elements of the lists to be joined must be strings; otherwise, each element must be converted to a string before using the method.
2. Combining lists of numbers
How you combine the numbers in a list depends on the result you want.
If you want to combine each number into a single string, you can use the join()
method.
If you want to get the sum of each number in the list, you can use the sum()
or reduce()
functions.
Let's take a look at each one.
2.1. Combining into a string
As we saw in Section 1, in order to use the join()
method, we need to convert each element of the list into a string.
In the example below, we'll use generator syntax to convert all scalar elements to strings.
numbers = [1, 2, 3, 4, 5]
combined = ''.join(str(num) for num in numbers)
print(combined)
# Output: 12345
We use str(num)
to convert each element into a string, and then we join them with the join()
method to make the numbers into a single string.
2.2. Finding the sum
There are many ways to sum the numbers in a list. Let's look at two of the most common ones.
First, you can simply sum them using the Python built-in function sum()
.
numbers = [1, 2, 3, 4, 5]
sum_of_numbers = sum(numbers)
print(sum_of_numbers)
# Output: 15
The second way is to write functional style code using the reduce()
function.
The reduce()
function is provided by the functools
module and provides the ability to repeatedly combine elements of a list to produce a single result.
You can use it to combine the numbers in a list into a single number.
Let's see it with an example.
from functools import reduce
numbers = [1, 2, 3, 4, 5]
reduce(lambda x,y: x + y, numbers)
print(combined)
# Output: 15
You can also write a new algorithm using the reduce()
function like this
from functools import reduce
numbers = [1, 2, 3, 4, 5]
combined = reduce(lambda x, y: x * 10 + y, numbers)
print(combined)
# Output: 12345
We used the function lambda x, y: x * 10 + y
to iteratively combine the elements into a single number.
Each element is multiplied by 10 and then added to the next element to create the result 12345
.
3. Combining lists within lists
There are many ways to combine two lists or lists within a list. Here are a few of them.
3.1. Using the +
operator
The easiest way to combine two lists is to use the +
operator.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2
print(combined)
# Output: [1, 2, 3, 4, 5, 6]
This method does not change the original list, it creates a new combined list.
3.2. Using the extend()
method
The extend()
method is a way to add items from another list to a list.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1)
# Output: [1, 2, 3, 4, 5, 6]
The extend()
method allows you to add elements from list2
to list1
. This method modifies list1
and does not create a new list.
3.3. Using the list concatenation operator
You can also combine two lists using the list concatenation operator *
.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = [*list1, *list2]
print(combined)
# Output: [1, 2, 3, 4, 5, 6]
This method also does not change the original list, it creates a new combined list.
4. Remove duplicates when merging lists
There are also several ways to remove duplicates when combining two lists. Here are a few examples
4.1. Use sets
Sets are characterized by not allowing duplicate items. You can take advantage of this by converting two lists to a set and then back to a list to get a deduplicated list.
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
combined = list(set(list1 + list2))
print(combined)
# Output: [1, 2, 3, 4, 5, 6, 7, 8]
First, we combine list1
and list2
using the +
operator.
Then we use the set()
function to convert the combined lists into a set, which we then convert into a list using the list()
function.
This way we get a list with duplicates removed.
4.2. How to use list compression
You can also use list compression to remove duplicates. To do this, we combine two lists and add a condition to filter out duplicates.
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
combined = [x for x in list1 + list2 if x not in list1 or x not in list2]
print(combined)
# Output: [1, 2, 3, 6, 7, 8]
After combining list1
and list2
with the +
operator,
Use list compression to create a list where each item contains only those items that are not in list1
and not in list2
. This will give you a list without duplicates.
5. Combining lists by index
Here's how to combine two or more lists using the map()
function and the zip
function.
The map()
function takes multiple lists and applies a function to combine the elements of each list, resulting in a new list.
The zip()
function bundles elements with the same index into a tuple.
Used together, these two functions produce results like the example below.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
combined = list(map(list, zip(list1, list2, list3)))
print(combined)
# Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
zip(list1, list2, list3)
packs the same indexed elements of each list into a tuple.
map(list, ...)
converts these tuples into lists.
So the variable combined
stores the sub-lists of list1
, list2{;Py}
and list3
with the same indexed elements.
Conclusion
In this article, we've answered many of your questions about combining lists in Python. We hope you find it useful when you need to combine elements of a list, or when you need to combine lists themselves.
