Python List append() Method Tutorial

Python List provides the append() method which is used to add a new item to the list. The append() method is provided via the list variable as a built-in method.

append() Method Syntax

The append() method has the following syntax where it only accepts single parameter.

LIST.eppend(ITEM)
  • LIST is the list where the ITEM will be added.
  • ITEM is a string, number, list, tuple etc. which will be added into the LIST.

Add Item To List

We will start with a simple example where we will add single and basic item which can be a string, number etc.

mylist = [ "ismail" , "baydan" , 1 , 2 ]

mylist.append("ahmet")

mylist.append("ali")

mylist.append(3)

mylist.append(4)

# Output
# ['ismail', 'baydan', 1, 2, 'ahmet', 'ali', 3, 4]

Add List To List

The append() method can be also used to add other than basic structures like list. When a list is provided to the append() method it will be added an item into the parent list.

mylist = [ "ismail" , "baydan" , 1 , 2 ]

mylist.append( [ 3 , 4 , 5 ] )

mylist.append( [ "ali" , "ahmet" ] )

print(mylist)

# Output
# ['ismail', 'baydan', 1, 2, [3, 4, 5], ['ali', 'ahmet']]

If we want to append all items into the parent list one by one separately the extend() method can be used.

mylist = [ "ismail" , "baydan" , 1 , 2 ]

mylist.extend( [ 3 , 4 , 5 ] )

mylist.extend( [ "ali" , "ahmet" ] )


print(mylist)

# Output
# ['ismail', 'baydan', 1, 2, 3, 4, 5, 'ali', 'ahmet']

Add Dictionary To List

The append() method can be used to add a dictionary into the existing list. On the following example we will append a dictionary as a new single item to the list.

mylist = [ "ismail" , "baydan" , 1 , 2 ]

mylist.append( { "name":"ahmet" , "surname":"baydan" } )


print(mylist)

# Output
# ['ismail', 'baydan', 1, 2, {'name': 'ahmet', 'surname': 'baydan'} ]

Leave a Comment