What is the difference between append() and extend() methods in Python?Rashid D
In Python, theappend()
andextend()
methods are used to add elements to a list, but they have distinct behaviors. Here's a detailed explanation of the differences between the two methods:
1. append() method: Theappend()
method is used to add a single element to the end of a list. It modifies the original list by adding the specified element as a single item.
1 2 3 4 5 6
my_list = [1, 2, 3] my_list.append(4) print(my_list) # Output: [1, 2, 3, 4]
In the example above, theappend(4)
statement adds the element4
to the end of the listmy_list
, resulting in[1, 2, 3, 4]
.
2. extend() method: Theextend()
method is used to add multiple elements to the end of a list. It takes an iterable (such as a list, tuple, or string) as an argument and appends each element of the iterable to the original list.
1 2 3 4 5 6
my_list = [1, 2, 3] my_list.extend([4, 5, 6]) print(my_list) # Output: [1, 2, 3, 4, 5, 6]
In this example, theextend([4, 5, 6])
statement appends each element[4, 5, 6]
individually to the end ofmy_list
, resulting in[1, 2, 3, 4, 5, 6]
.
It's important to note that when usingextend()
, the elements of the iterable are added individually to the list, whereasappend()
adds the entire iterable as a single element.
1 2 3 4 5 6 7 8 9 10 11 12
my_list = [1, 2, 3] another_list = [4, 5, 6] my_list.append(another_list) print(my_list) # Output: [1, 2, 3, [4, 5, 6]] my_list = [1, 2, 3] another_list = [4, 5, 6] my_list.extend(another_list) print(my_list) # Output: [1, 2, 3, 4, 5, 6]
As shown in the example,append()
adds the entireanother_list
as a single element, whereasextend()
adds each element ofanother_list
individually tomy_list
.
In summary,append()
adds a single element to the end of a list, whileextend()
adds multiple elements from an iterable to the end of a list. Choose the appropriate method based on your specific needs and the desired outcome.
Similar Questions
What is the difference between a class method and a property in Python?
What is the difference between a method and a function in Python?
What is the difference between == and is in Python?
What is the difference between a private method and a public method in Python?
What is the difference between is and == in Python?
What is the difference between print() and return in Python?
What is the difference between read() and readline() in Python?
What is the difference between a metaclass and a class in Python?
What is the difference between a class and a metaclass in Python?
What is the difference between __del__() and __exit__() in Python?
What is the difference between a module and a package in Python?
What is the difference between a package and a module in Python?
What is the difference between strip() and rstrip() in Python?
What is the difference between a class attribute and a class method in Python?
What is the difference between a class attribute and a class method in Python?
What is the difference between a class attribute and a class method in Python?
What is the difference between a class method and a static method in Python?
What is the difference between a class method and a static method in Python?
What is the difference between a class method and a static method in Python?
What is the difference between a class attribute and a static method in Python?