extend() method in Python List

It is a method in list that adds iterable elements (string, list, tuple etc.) to the end of the list. It modifies the current list. It doesn’t return a value.

Syntax: list.extend(iterable_element)

Don’t know what is count method in python? Read: count() method in Python List

Example 1:

s=["Name","codewindow","Country","India"]
s2=["english","hindi"]
s.extend(s2)
print(s)

Output:

[‘Name’, ‘codewindow’, ‘Country’, ‘India’, ‘english’, ‘hindi’]

Explanation:

Here the method iterated through every element of the list “s2” and extends it by adding it to the end of the parent list.

Example 2:

s=["Name"]
s1=("codewindow", "Country", "India")
s2={"english", "hindi"}
s.extend(s1)
s.extend(s2)
print(s)

Output:

[‘Name’, ‘codewindow’, ‘Country’, ‘India’, ‘hindi’, ‘english’]

Explanation:

Here every iterable element in tuple “s1” is added to the parent list in the end and then every iterable element of the set “s2” is added to the end of the parent list.


You Missed