Python List append() Method Example | Python Standard Libary
Python List append() is an inbuilt method that adds a single element to the existing list. It doesn’t return a new list; instead, it modifies the original list. Python has a great built-in list type named “list.” List literals are written within square brackets [ ]. Lists work similarly to strings, use the len() function and square brackets [ ] to access data, with the first element at index 0. The append() method in Python appends an element or item to the end of the list. The list append() method
Python List append() Method Example
The syntax of the append() method is the following.
The list append() method takes a single element and adds it to the end of the list. An element can be numbers, strings, another list, dictionary, etc.
Adding Element to a List in Python
Let us create a file called app.py and add the following code inside it.
// app.py GoT = ['Daenerys', 'Jon', 'Tyrion'] Friends = 'Rachel' GoT.append(Friends) print(GoT)
Save the file and go to the cmd or terminal and run the file using the following command.
Adding List to a List in Python
Now, write the following code inside the app.py file.
GoT = ['Daenerys', 'Jon', 'Tyrion'] Friends = ['Rachel', 'Monica', 'Phoebe'] GoT.append(Friends) print(GoT)
In the above example, we are adding a list to the list. So the second list considers as a single list item and it appends as a separate item. The output is the following.
Again, it does not return a new list, modifies the original.
Finally, Python List append() Example Tutorial is over.