TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python List extend() method with Examples

Python List extend() method with Examples on append(), clear(), extend(), insert(), pop(), remove(), index(), count(), pop(), reverse(), sort(), copy(), all(), bool(), enumerate(), iter(), map(), min(), max(), sum() etc.

<< Back to PYTHON

Python List extend() Method

Python extend() method extends the list by appending all the items from the iterable. Iterable can be a List, Tuple or a Set. Examples are described below.

Signature

extend(iterable)

Parameters

x: Iterable type parameter.

Return

It does not return any value rather modifies the list.

Let's see some examples of extend() method to understand it's functionality.

Python List extend() Method Example 1

It is a simple example to describe the use of extend method.

# Python list extend() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
list.extend('4')
print("After extending:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After extending:
1
2
3
4

Python List extend() Method Example 2

We can pass list as an element and the list will be extended. See the example below.

# Python list extend() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
list2 = ['4','5','6']
list.extend(list2)
print("After extending:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After extending:
1
2
3
4
5
6

Python List extend() Method Example 3 : Tuple

It can be possible that the element is a tuple type and the list extends itself by tuple elements. See the example below.

# Python list extend() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
tuple = ('4','5','6')
list.extend(tuple)
print("After extending:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After extending:
1
2
3
4
5
6

Python List extend() Method Example : Set

It can be possible that the element is a Set type and the list extends itself by Set elements. See the example below.

# Python list extend() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
set = {'4','5','6'}
list.extend(set)
print("After extending:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After extending:
1
2
3
6
5
4

Next TopicPython Lists




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf