In this Python tutorial, we will learn about the Python Append List to another List without Brackets where we will see various ways to Append a List to another List without Brackets using the examples.
Python append list to another list
Python programmers often encounter – appending one list to another. To start, let’s understand what appending a list in Python usually means.
When we talk about appending a list to another list in Python, it usually involves adding the second list as a single element at the end of the first list. This is done using the append()
method. However, this method results in a nested list, i.e., a list within a list.
Example:
east_states = ['New York', 'Florida', 'Georgia']
west_states = ['California', 'Washington', 'Nevada']
east_states.append(west_states)
print(east_states)
Output:
Notice the extra brackets around the second list in the output. This means that east_states is nested inside west_states. Sometimes, you want to merge two lists without nesting, meaning you want to add each element of the second list to the first list as an individual element.
So, let’s move ahead and learn different ways to append List to another List without Brackets.
Append List to another List without Brackets in Python
Today, we’re going to dive into a common scenario American developers often deal with: adding one list to another, but without the added brackets that typically result.
To kick off, let’s imagine that we’re on a road trip, traveling through various states. We have two lists of states, one for the eastern states we’ve visited, and one for the western states.
But, this time we just want one big list of all states we’ve visited, without categorizing them as east or west. So, let’s discuss each of the methods:
Method1: Using Python Extend() Function
east_states = ['New York', 'Florida', 'Georgia']
west_states = ['California', 'Washington', 'Nevada']
east_states.extend(west_states)
print(east_states)
Output:
Now you can see, extend()
adds each element of east_states to west_states resulting in a single list without extra brackets. In other words, extend()
merges the two lists together. We’ve effectively created a list of all states visited during our road trip!
Read How to remove duplicate elements from a List in Python
Method2: +=
operator in Python
An alternative to the extend()
method is the +=
operator, which can be used to achieve the same effect.
east_states = ['New York', 'Florida', 'Georgia']
west_states = ['California', 'Washington', 'Nevada']
east_states += west_states
print(east_states)
Output:
As you can see, the +=
operator also combines the lists into one, just like extend()
, without any extra brackets or nesting.
Conclusion
In conclusion, Python provides different methods to append one list to another. While append()
nests the second list within the first, extend()
and +=
operator adds the second list to the first as individual elements, removing the need for additional brackets.
You may also like to read the following Python tutorials.
- Get current directory Python
- Create a tuple in Python
- Add Elements in List in Python Using For Loop
- How to check if the list is empty in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.