Trending September 2023 # Different Examples Of Python Extend # Suggested October 2023 # Top 15 Popular | Nhahang12h.com

Trending September 2023 # Different Examples Of Python Extend # Suggested October 2023 # Top 15 Popular

You are reading the article Different Examples Of Python Extend updated in September 2023 on the website Nhahang12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Different Examples Of Python Extend

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

array_one.extend(array_two) Examples of Python Extend

Examples of python extend are:

Example #1 – Extending an Array

Code:

import array as arr arr1 = arr.array('i', [1,2,3,4]) arr2 = arr.array('i', [5,6,7]) arr1.extend(arr2) print(arr1)

Output:

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

Code:

a = [1,2,3,4,5] b = [6,7] a.extend(b) print(a)

Output:

Example #3

Code:

a = [2010,2011,2012] b = ['john','doe',2013] a.extend(b) print(a)

Output:

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.

Example #4

Code:

a = [2010,2011,2012] a.extend(5) print(a)

Output:

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.

Example #5

Code:

a = [2010,2011,2012] a.extend("John") print(a)

Output:

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.

Conclusion

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.

Recommended Articles

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 –

You're reading Different Examples Of Python Extend

Update the detailed information about Different Examples Of Python Extend on the Nhahang12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!