Python list Method - extend()

www.igift‮edi‬a.com

The extend() method in Python is a built-in method used to add elements of an iterable (list, tuple, set, dictionary) to the end of an existing list.

Syntax:

list_name.extend(iterable)

Example:

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

In the above example, the extend() method is used to add elements of the list [4, 5, 6] to the end of the existing list my_list. The method modifies the original list and returns None.

The extend() method can also be used to append elements of a tuple, set or dictionary to the end of an existing list. If the iterable contains mutable objects like lists, dictionaries, etc., then the objects are appended as a whole (not as individual elements).