Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.
Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.
For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Data Science and Machine Learning - 16 wks
Full-Stack Software Developer - 16w
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
Here is a simple way to add elements to a list using Python List Extend Method:
You can also assign the new elements that you want to add to the list to a new variable as follows:
This is a simple way to add elements to a list, let's see more information on how the Python list extend method works and its use cases in the following sections.
Sometimes we will need to add items from one list to another, and by adding items we mean adding those items one by one to our desired list. Why are we telling you this? You might already be familiarized with the
.append() method to add elements to a list, this method takes an argument and adds it as one more element to our original list, it means that, if we pass a list as an argument, it will be added as a list inside our original list, like this:
See that the
extra_brands list was added as a list inside the
brands list instead of just adding the
extra_brands list elements one by one. Here is where we use the extend method as follows:
Now the elements of the
extra_brands list were individually added to the
brands list, and not as a "list inside a list". This is since the extend method iterates the argument that it takes, this is called the iterable (
.extend(iterable)) and adds each element of that argument iterating through all of the argument elements.
The extend method also accepts other sequences besides a list, in this case, let's see how adding a string using the extend method works. Remember that a string is a chain of characters, so when we use the extend method, all the characters of the string will be added to the list as individual elements. Let's see an example as follows:
If you'd like to add the whole string as a new element to any list, it is best to use
.append() so you would have:
If we try to extend a list with any integer, we will get an error since an integer is not an iterable element, it is not a sequence of elements where we can iterate each one of them. So, we will get a
TypeError: 'int' object is not iterable. We cannot add an integer to a list using the extend method, but don't panic, we can still use the
.append() method to add it without problems as follows:
Adding a Tuple or a Set to a list using the Python list extend method works the same way as adding a list. Both Sets and Tuples are chains of elements that can be iterable in the same way as lists, if we use the extend method, all elements of a Set or Tuple will be added to our desired list. Let's see an example below:
We can either add the dictionary keys or values using the Python list extend method into any list, if we just want to add the dictionary keys, we just need to pass our dictionary variable to the extend method and if we want to add the dictionary values, we need to pass our dictionary variable alongside
.values(), like this
list.extend(dictionary.values()). Let's see an example below:
In this article we could see that the Python List Extend method is used in different ways depending on the type of data we are working with in our projects, make sure you are aware of this in your future projects!