Difference between append vs. extend list methods in Python

0 votes

What's the difference between the list methods append() and extend()?

Aug 8, 2018 in Python by Priyaj
• 56,520 points
85 views

1 answer to this question.

0 votes

append: Appends object at the end.

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

gives you: [1, 2, 3, [4, 5]]


extend: Extends list by appending elements from the iterable.

x = [1, 2, 3] x.extend([4, 5]) print (x)

gives you: [1, 2, 3, 4, 5]

answered Aug 8, 2018 by bug_seeker
• 15,350 points

Related Questions In Python

+1 vote
2 answers

Difference between append vs. extend list methods in Python

Python append() method adds an element to ...READ MORE

answered 1 day ago in Python by germyrinn
• 180 points
166 views
0 votes
1 answer

In Python, what is difference between Array and List?

Lists and arrays are used in Python ...READ MORE

answered Mar 15 in Python by Taj
• 1,040 points

edited Mar 18 by Omkar 1,449 views
0 votes
1 answer

Difference between two lists in python

difference = list(set(list1) - set(list2)) READ MORE

answered May 24, 2018 in Python by Nietzsche's daemon
• 4,260 points
439 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 172 views
+4 votes
6 answers
0 votes
1 answer

Create an empty list in python with certain size

Try this instead: lst = [None] * 10 The ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,350 points
129 views
0 votes
1 answer

When to use %r instead of %s in Python? [duplicate]

The %s specifier converts the object using ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,350 points
54 views