Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference Between ‘+’ and ‘append’ in Python with examples
In Python, the + operator is used to concatenate two lists or strings together and return a new object, whereas the append() method is used to add elements to the end of an existing list. The + acts as an operator whereas append() is a method. In this article, we will understand the differences between the + operator and the append() method in Python.
Key Differences
| Aspect | + operator | append() method |
|---|---|---|
| Purpose | Concatenation | Adds an element to the end |
| Type | Operator | Method |
| Input | Two or more strings/lists | One element |
| Output | New concatenated string/list | None |
| Changes original object | No | Yes |
The + Operator
The + operator concatenates two strings or lists together and returns a new object. The original objects remain unchanged. This creates a completely new object in memory.
Example with Lists
# Concatenating two lists using '+' operator
list1 = [1, 2, 3]
list2 = [4, 5, 6]
new_list = list1 + list2
print("Original list1:", list1)
print("Original list2:", list2)
print("New concatenated list:", new_list)
Original list1: [1, 2, 3] Original list2: [4, 5, 6] New concatenated list: [1, 2, 3, 4, 5, 6]
Example with Strings
# Concatenating two strings using '+' operator
string1 = "Hello"
string2 = "World"
new_string = string1 + " " + string2
print("Original string1:", string1)
print("Original string2:", string2)
print("New concatenated string:", new_string)
Original string1: Hello Original string2: World New concatenated string: Hello World
The append() Method
The append() method adds a new element at the end of the original list. The original list is modified in-place and no new object is created. It returns None.
Adding Single Elements
# Adding a single element using append() method
my_list = [1, 2, 3]
print("Original list:", my_list)
my_list.append(4)
print("After append(4):", my_list)
my_list.append(5)
print("After append(5):", my_list)
Original list: [1, 2, 3] After append(4): [1, 2, 3, 4] After append(5): [1, 2, 3, 4, 5]
Appending a List as Single Element
# Adding a list to an existing list using append() method
my_list = [1, 2, 3]
new_list = [4, 5, 6]
my_list.append(new_list)
print("After appending a list:", my_list)
print("Length of my_list:", len(my_list))
After appending a list: [1, 2, 3, [4, 5, 6]] Length of my_list: 4
When to Use Which?
Use + when you need to combine data without modifying the original objects. Use append() when you want to modify an existing list by adding elements to it.
# Memory efficiency comparison
numbers = [1, 2, 3]
# Using + creates new object (less memory efficient for large lists)
result1 = numbers + [4, 5]
print("Using +:", result1)
print("Original numbers:", numbers)
# Using append modifies existing list (more memory efficient)
numbers.append(4)
numbers.append(5)
print("Using append:", numbers)
Using +: [1, 2, 3, 4, 5] Original numbers: [1, 2, 3] Using append: [1, 2, 3, 4, 5]
Conclusion
The + operator creates new objects by concatenating sequences, while append() modifies existing lists in-place. Use + for combining data without side effects, and append() for memory-efficient list building.
