In this Python post, we’ll learn how we can properly use Python list extend method in our code. We will be going through multiple examples to practically understand the working and usage of extend method in Python list.
After reading this post, I am sure you’ll have in-depth practical knowledge of how to use Python list extend method.
So why wait more for that? Let’s get right into its implementation phase.
What is Python List Extend?
The extend method of Python list is used to add items to the end of an existing list. These items can be a list, tuple, string etc. We’ll now practically understand how to add items using the extend method.
Syntax of Extend Method
listName.extend( iterable )
We can pass string, list, tuple or other iterable to this method so it can add these items to the end of specified list.
Implementing Python List Extend Method (Multiple Examples)
Let’s now look at multiple examples on how use Python list extend.
Example 1: Adding List Using Extend Method
For that, we will first create a Python list and then create another list so we can add the second one to the first one using extend method. See below code:
list1Items= [1,2,3,5,6] list2Items= [23,5,63] list1Items.extend(list2Items) // adding list 2 to list 1 using extend print(list1Items) // [1, 2, 3, 5, 6, 23, 5, 63]
As you can see here, all the items of the second list are is added to the first list. Unlike append method, which would have added the complete list as one item. See below:
list1Items= [1,2,3,5,6] list2Items= [23,5,63] list1Items.append(list2Items) // adding list 2 to list 1 using append print(list1Items) // [1, 2, 3, 5, 6, [23, 5, 63]]
Example 2: Add String To List Using Extend Method
list1Items= [1,2,3,5,6] list1Items.extend('Green color') // adds every item of string to list print(list1Items) // [1, 2, 3, 5, 6, 'G', 'r', 'e', 'e', 'n', ' ', 'c', 'o', 'l', 'o', 'r']
You can see that the Python list extend method took every character from the string and added it to the list.
Example 3: Extend Method Returns None
The extend method does not return a new list, it just update the current list with new items. It doesn’t store any items. See below code:
list1Items= [1,2,3,5,6] list2Items= [23,5,63] newList= list1Items.extend(list2Items) print(newList) // none
The new variable returns none which means the extend method just update the list and doesn’t store or return anything.
Example 4: Adding Set and Tuple To List
Let’s now add a set and tuple in our existing list. See below code:
list1Items= [1,2,3,5,6] tupleItems = (23,53,'green') // tuple items setItems = {'purple',24.5} // set items list1Items.extend(tupleItems) // adding tuple to existing list print(list1Items) // [1, 2, 3, 5, 6, 23, 53, 'green'] list1Items.extend(setItems) // adding tuple to existing list print(list1Items) // [1, 2, 3, 5, 6, 23, 53, 'green', 24.5, 'purple']
You can see here that the tuple and set is also added successfully to the existing list.
So this is how you can use Python list extend method in your own Python code with ease.
If you want to know how to add items to list using append method then click here. I’ve written a detailed article on that as well.
Do ask if you still have any questions related to the usage of Python list extend method. I’d be happy to answer all your questions.
Conclusion
To conclude this Python post, hope you now have a detailed practical understanding of how to use Python list extend method. I’d love to have your feedback on this post. Thank you for reading.