Pyton

Python List Append

Python List Append

The list is one of the important data structures in Python that arranges the elements in a sequence. The append() is the built-in function in Python that inserts the element or items to the end of the list and essentially increases the length of the list by one. This article explains the Python list append() function with examples.

Syntax of the append() function

The syntax of the append() function is as follows:

listObj.append(item)

The append() function is called with the name of the list object. It only takes one parameter. We pass the item as an argument.

Examples

#creating a list of numbers
myList = [1,2,3,4,5,6]
#apending the list
myList.append(7)
#printing the updated list
print("The updated list is:", myList)
#creating a list of strings
myList = ['a','b','c','d']
#apending the list
myList.append('e')
myList.append('f')
myList.append('g')
#printing the updated list
print("The updated list is:", myList)
#creating a list of mix data types
myList = ['a',1,2,3,'b','c','d',5]
#apending the list
myList.append(6)
myList.append('Kamran')
myList.append('Sattar')
myList.append('Awaisi')
#printing the updated list
print("The updated list is:", myList)

Output

In the output it can be observed that all the items appended in the lists are added to the end of the lists.

We can also append a list in another list. The appended list be added at the end. Let's append a list in another list.

#creating a list of numbers
myList1 = [1,2,3,4,5,6]
#creating a list of strings
myList2 = ['a','b','c','d']
# appending the list
myList1.append(myList2)
print(myList1)

Output

Conclusion

Python provides many built-in data structures like lists, tuples, dictionaries, etc. The list arranges the items in sequence. We can add the elements in the list after declaring the list. The append() is a built-in function that inserts the items at the end of the list. This article explains the list append() function with examples.

Gry How to Show FPS Counter in Linux Games
How to Show FPS Counter in Linux Games
Linux gaming got a major push when Valve announced Linux support for Steam client and their games in 2012. Since then, many AAA and indie games have m...
Gry How to download and Play Sid Meier's Civilization VI on Linux
How to download and Play Sid Meier's Civilization VI on Linux
Introduction to the game Civilization 6 is a modern take on the classic concept introduced in the series of the Age of Empires games. The idea was fai...
Gry How to Install and Play Doom on Linux
How to Install and Play Doom on Linux
Introduction to Doom The Doom Series originated in the 90s after the release of the original Doom. It was an instant hit and from that time onwards th...