Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python3
append
extend
communitycreator

What is the difference between Python's list append and extend?

Gutha Vamsi Krishna

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.

Python provides methods like append and extend to add elements to the existing list.

In this shot, we will go through the differences between the methods append() and extend().

append()

Python’s append() method is used to add elements to the existing list.

It will append the object to the end of the list.

Syntax

list.append(element)

Parameters

The append() method will accept an element as a parameter, and it will add that to the list.

In the following code snippet:

  • Line 1: Initializes a list fruits with ["apple","banana","grapes"].
  • Line 2: Prints the list before using the append() method.
  • Line 3: Adds an element orange to the end of the list with the append() method.
  • Line 4: Prints the list after appending a new element.
fruits = ["apple","banana","grapes"]
print("list of fruits before append -> " + str(fruits))
fruits.append("orange")
print("list of fruits after append -> " + str(fruits))

If we try to append a list to an existing list, the whole list will be treated as one element and it appends as one.

In the following code snippet:

  • Line 8: Appending the entire list ["peach","mango","watermelon"] to fruits list will be treated as one element, and will be added at the end as ['apple', 'banana', 'grapes', ['peach', 'mango', 'watermelon']].
#initialization of list
fruits = ["apple","banana","grapes"]
#print before appending
print("list of fruits before append -> " + str(fruits))
#appending entire list
fruits.append(["peach","mango","watermelon"])
#print after appending
print("list of fruits after append -> " + str(fruits))

extend()

The extend() method acts the same as the append() method if we add a single element, but it behaves differently if we add a list.

  • It will expand the original list, and add elements from the list.

In the following code snippet:

  • Line 8: Extending the fruits list with ["peach","mango","watermelon"] will result in ['apple', 'banana', 'grapes', 'peach', 'mango', 'watermelon'], as the extend() method adds elements provided to it individually to the fruits list.
#initialization of list
fruits = ["apple","banana","grapes"]
#print before extend
print("list of fruits before extend -> " + str(fruits))
#appending entire list
fruits.extend(["peach","mango","watermelon"])
#print after extend
print("list of fruits after extend -> " + str(fruits))

RELATED TAGS

python3
append
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