But we have executed the above using the append method then we will get the desired result. The output might not be as we wanted as it has separated every character of the string. Extend method treats string value as a collection of characters, and they are iterable.
Example #5Īs you can see in the above example, we have passed a string value instead of a list, but still, it works. 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. In the output, you can see all the elements of list b are extending into list a.Īs 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”. Then we are extending list ‘a’ with the elements of list ‘b’. Both the list have an integer type of elements. In the above program, we have created two lists, a and b.
We have passed both the array and then in an output you can see that we have elements of both the arrays. We are using arr1 as the main array because we want to extend arr1 with elements of arr2. In the above program, we have imported the array module, and we have given an alias name as ‘arr’ to it. Syntax: array_one.extend(array_two) Examples of Python ExtendĮxamples of python extend are: Example #1 – Extending an Array