DEV Community

SATHISHKUMAR R
SATHISHKUMAR R

Posted on

Whats is Append vs Extend in python?

1.append() in Python:

append() adds one element to the end of a list.

Example:

fruits = ["Apple", "Banana"]fruits.append("Orange")print(fruits)

Output:
['Apple', 'Banana', 'Orange']

Even if you add multiple values inside a list, append() treats them as one element.

numbers = [1, 2, 3
]numbers.append([4, 5])
print(numbers)

Output:
[1, 2, 3, [4, 5]]

  1. extend() in Python:

extend() adds multiple elements individually to the end of a list.

Example:

fruits = ["Apple", "Banana"]

fruits.extend(["Orange", "Mango"])

print(fruits)

Output:
['Apple', 'Banana', 'Orange', 'Mango']

Top comments (0)