TheDeveloperBlog.com

Home | Contact Us

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

Python List append() method with Examples

Python List append() 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 append() Method

Python append() method adds an item to the end of the list. It appends an element by modifying the list. The method does not return itself. The item can also be a list or dictionary which makes a nested list. Method is described below.

Signature

append(x)

Parameters

x: It can be a number, list, string, dictionary etc.

Return

It does not return any value rather modifies the list.

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

Python List append() Method Example 1

First, let's see a simple example which appends elements to the list.

# Python list append() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
# Appending element to the list
list.append(4)  
print("List after appending element : ",list) # Displaying list

Output:

1
2
3
List after appending element :  ['1', '2', '3', 4]

Python List append() Method Example 2

Appending a list to the list is also possible which will create a list inside the list. See the example below.

# Python list append() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
# Appending a list to the list
list2 = ['4','5','6','7']
list.append(list2)  
print("List after appending element : ", list) # Displaying list

Output:

1
2
3
List after appending element :  ['1', '2', '3', ['4', '5', '6', '7']]

Python List append() Method Example 3

Appending multiple lists to the list will create a nested list. Here, two lists are appended to the list and generates a list of lists. See the example below.

# Python list append() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
# Appending a list to the list
list2 = ['4','5','6','7']
list.append(list2)  
# Nested list
list2.append(['8','9','10']) # Appending one more list
print("List after appending element : ", list) # Displaying list

Output:

1
2
3
List after appending element :  ['1', '2', '3', ['4', '5', '6', '7', ['8', '9', '10']]] 

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