Python list extend Function | Adding all items of a list example

Python extend() function is used to adds the specified list elements (or any iterable) to the end of the current list. In simple word can say extend() method appends the contents of seq to list.

Note: Python list extend returns none, only modifies (Add new elements) the original list.

Syntax

Parameter Values

Required single argument (a list) and adds it to the end.

Return Value

It doesn’t return any value, only modifies the original list.

Python list extend examples

We will do some simple examples and with different types of Iterable like- List, tuple, set, etc.

Adding all items of a list

Add the elements of programming to the language list:

Output:

Extended List: [‘French’, ‘English’, ‘German’, ‘python’, ‘Java’]

Add Elements of Tuple List

Output:

New Language List: [‘French’, ‘English’, ‘German’, ‘Spanish’, ‘Portuguese’]

Add Elements of Tuple List

Example of Add a tuple to the fruits list.

Output:

[‘apple’, ‘banana’, ‘cherry’, 1, 3, 5, 7]

Possible to Python extend multiple lists?

Yes, it is Possible to append multiple lists at once in python.

or

or even

Python list extend vs +

The only difference on a bytecode level is that the .extend way involves a function call, which is slightly more expensive in Python than the INPLACE_ADD.

It will not affect unless performing this operation billions of times. It is likely, however, that the bottleneck would lie someplace else.

Q: What is the Difference between append and extend in python?

Answer: Function append and extend in python are:-

append: Appends object at the end.

gives you: [1, 2, 3, [4, 5]]

extend: Extends list by appending elements from the iterable.

gives you: [1, 2, 3, 4, 5]

Q: How to add lists using + or += operator?

Answer: Example of add items of a list to another list using + or += operator.

Output: [‘A’, ‘B’, 3, 4]

Do comment if you have any queries and suggestions on method list.extend Python 3.

Note:
IDE: PyCharm 2020.1.1 (Community Edition)
macOS 10.15.4
Python 3.7
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.