What is the difference between append() and extend()?
In Python, understanding the difference between append() and extend() is essential—especially when preparing for the Best Python Certification programs, where list operations are frequently tested.
-
append()adds a single element to the end of a list. -
extend()adds multiple elements from an iterable (list, tuple, set, etc.) to the existing list.
Example:
a = [1, 2]
a.append([3, 4]) # Result: [1, 2, [3, 4]]
a.extend([3, 4]) # Result: [1, 2, 3, 4]
Use append() when you want to add one item, and use extend() when you want to merge multiple elements into a list.
-
How does Python determine whether a variable is local or global?
4 weeks ago
-
What is the purpose of virtual environments in Python?
1 month ago
-
What is the difference between np.zeros(), np.ones(), and np.empty()?
2 months ago
-
Which Python certification focuses on real-world projects?
2 months ago
-
What are Python’s data structures?
2 months ago
Latest Post: What SQL case study questions are asked in senior data analyst interviews? Our newest member: Pankaj12 Recent Posts Unread Posts Tags
Forum Icons: Forum contains no unread posts Forum contains unread posts
Topic Icons: Not Replied Replied Active Hot Sticky Unapproved Solved Private Closed