KnowledgeBoat Logo
|
LoginJOIN NOW

Computer Science

Differentiate between append() and extend() methods of list.

Python List Manipulation

1 Like

Answer

append()extend()
The append() method adds a single item to the end of the list. The single element can be list, number, strings, dictionary etc.The extend() method adds one list at the end of another list.
The syntax of append() method is list.append(item).The syntax of extend() method is list.extend(list1).
For example:
lst1 = [10, 12, 14]
lst1.append(16)
print(lst1)
Output — [10, 12, 14, 16]
For example:
t1 = ['a', 'b', 'c']
t2 = ['d', 'e']
t1.extend(t2)
print(t1)
Output — ['a', 'b', 'c', 'd', 'e']

Answered By

2 Likes


Related Questions