Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
list
extend()
communitycreator

What is List.extend() in Python?

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The extend() method adds the specified iterable to the end of the given list.

expand() iterates over an iterable, adding and extending the list with each element of the iterable.

Syntax


list.extend(iterable)

Parameter

The extend() method requires an iterable (list, set, tuple, etc).

Code

The following code shows how to use the extend() method:

Example 1: Adding a tuple to a list

fruits = ['apple', 'banana', 'Avocado']
numbers = (1, 6, 3)
# Using extend() method
fruits.extend(numbers)
print(fruits)

Example 2: Adding a string to another list

A string is iterable. If you extend a list with a string, each character of the string will be appended as you iterate over it.

# Creationg a list
my_list = ['Banana', 'Mango', 7,3]
my_list.extend('Apple') #using extend()
print(my_list)

RELATED TAGS

python
list
extend()
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring