Introduction to Python Extend
Python extends is a great feature in python. It’s a keyword as well as a method also. We can use python extend to extend any class and its features into existing code. It is also called inheritance in the object-oriented paradigm. Extend is also an inbuilt method in python that can be used with a list or array to extend its items. It is almost a similar append method to the list.
When we use the extend method for array, it will take two arrays and append another array at the end of the first array. Append also works in a similar way, but it creates a nested array in the case of append, while the extend method does not create a nested array; it adds elements of the second array in the first array.
Examples of Python Extend
Examples of python extend are:
Example #1 – Extending an Array
import array as arr arr1 = arr.array('i', [1,2,3,4]) arr2 = arr.array('i', [5,6,7]) arr1.extend(arr2) print(arr1)
In the above program, we have imported the array module, and we have given an alias name as ‘arr’ to it. We created two array arr1, arr2. These are of an integer type of array. Then we have used the extend method. We are using arr1 as the main array because we want to extend arr1 with elements of arr2. We have passed both the array and then in an output; you can see that we have elements of both the arrays.
Example #2 – Extending a List
a = [1,2,3,4,5] b = [6,7] a.extend(b) print(a)
In the above program, we have created two lists, a and b. Both the list have an integer type of elements. Then we are extending list ‘a’ with the elements of list ‘b’. In the output, you can see all the elements of list b are extending into list a.
a = [2010,2011,2012] b = ['john','doe',2013] a.extend(b) print(a)
In the above program, you can see that we have created the two lists; in the second list, we have added both integer and string values. The Extend method works well with both integer and string elements in the same list. As you can see in the output first list is extended.
Extend method only works if the items passed are iterable. Iterable means it can be looped. If the list is having a single element, then also it is iterable. But we have tried to pass the value directly inside the extend method; then, it will generate errors.
a = [2010,2011,2012] a.extend(5) print(a)
As you can see, we have passed value instead of a list in the extend method, and it has returned an error “object is not iterable”. The value we have passed is an integer, but if we have passed a string value, then extend method will work fine because the string is a collection of characters.
a = [2010,2011,2012] a.extend("John") print(a)
As you can see in the above example, we have passed a string value instead of a list, but still, it works. Extend method treats string value as a collection of characters, and they are iterable. So we can say that strings are iterable. The output might not be as we wanted as it has separated every character of the string. But we have executed the above using the append method then we will get the desired result. The append method won’t separate each string.
Python extends feature is a very useful method when we already have an array or list, and we want to add more elements inside it. With the extend method, we can easily do it without doing any kind of element and inserting element one by one. It will add all the elements in one go.
This is a guide to Python Extend. Here we discuss the Introduction of python extend along with different examples and its code implementation. you may also have a look at the following articles to learn more –