What is the difference between append() and extend() in Python lists?

88 views

What is the difference between append() and extend() in Python lists?

What is the difference between append() and extend() in Python lists?

solveurit24@gmail.com Changed status to publish February 13, 2025
0
  • append(): Adds a single element to the end of the list.
  • extend(): Extends the list by appending elements from an iterable.

Example:

my_list = [1, 2, 3]

# append()
my_list.append(4)
print(my_list)  # Output: [1, 2, 3, 4]

# extend()
my_list.extend([5, 6])
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]

solveurit24@gmail.com Changed status to publish February 13, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.