Sometimes the users want to add more items to their list. The python append function is used to add items to the end of the list. The user takes the item to be added as input from the user and adds it to the end.
For example:
my_currencies = ['Dollar', 'Euro', 'Pound', 'Dinar', 'Riyal', 'Rupees']
# append the new item to the end of the currencies list
my_currencies.append('Yen')
print(my_currencies)
Output:
['Dollar', 'Euro', 'Pound', 'Dinar', 'Riyal', 'Rupees', 'Yen']
Syntax:
list.append(item)
append() Parameters
The append() takes a single parameter as input from the user.
- item- this is the item that the user wants to be added at the end of the list.
Return Value from append()
The python append does not return any value to the user. It adds the item to the list and updates it.
Let us look at some more examples:
Example 1: Adding Element to a List
In the example given below, we are adding a new item to be appended to the end of the list.
Source Code:
my_animals = ['cat', 'dog', 'rabbit', 'frog', 'birds']
# Add 'horse' to the list
my_animals.append('horse')
print('Updated list of animals: ', my_animals)
Output
Updated list of animals: ['cat', 'dog', 'rabbit', 'frog', 'birds', 'horse']
Example 2: Adding List to a List
In python, users can also append a list to another list. Look at the program given below:
Source Code:
animals = ['cat', 'dog', 'rabbit', 'frog', 'birds' ]
# list of wild animals
wild_ani = ['tiger', 'fox', 'lion']
# appending the wild_ani list
animals.append(wild_ani)
print('Updated list of animals: ', animals)
Output
Updated list of animals: ['cat', 'dog', 'rabbit', 'frog', 'birds', ['tiger', 'fox', 'lion']]
How do you append to a list in Python?
The append() in python allows users to append items to their list.
For example:
my_list = ['Live', 'A', 'Good']
my_list.append('Life')
print my_list
Output:
['Live', 'A', 'Good', 'Life']
How do you append to a list?
Users can append a list in python using the in-built append(). This method adds the elements to the end of the list and updates it.
How do you append to a file in python?
To append a file in Python, you use can use ‘a’ access mode. This opens the file for writing. The data will be added to the end.
For example:
f1 = open("myfile.txt", "w")
L = ["This is Delhi \n", "This is Paris \n", "This is London"]
f1.writelines(L)
f1.close()
# Append-adds at last
f1 = open("myfile.txt", "a") # append mode
f1.write("Today \n")
f1.close()
f1 = open("myfile.txt", "r")
print("Output of Headlines after appending")
print(f1.read())
print()
f1.close()
# Write-Overwrites
f1 = open("myfile.txt", "w") # write mode
f1.write("Tomorrow \n")
f1.close()
f1 = open("myfile.txt", "r")
print("Output of Headlines after writing")
print(f1.read())
print()
f1.close()
Output:
The output of Headlines after appending
This is Delhi
This is Paris
This is London
today
The output of Headlines after writing
Tomorrow
How do you append in Python 3?
To append an item to the end of the list in python 3 we use the append().
For example:
langs = ['C++', 'Java', 'Python']
langs.append('C#')
print ("updated langs list : ", langs)
Output:
updated langs list : ['C++', 'Java', 'Python', 'C#']
Leave a Reply