What is Python append used for?
The Python append method adds elements to the end of a list. We can also append to a file when we are writing to a file.
Appending to a list with Python append method
Suppose we have a simple list, mylist = ‘elephant’, ‘giraffe’, ‘monkey’]. Now if we wanted to append something to this list we would need to call the name of the list, then using the dot operator we would add the method Python append followed by parentheses.
Here is the original list before we append anything:
Anything that we put inside the parentheses is going to be appended to the end of our list. For instance, if we can append the string ‘tiger’ to the above list as shown below:
Appending integers with Python append method
Apart from appending strings, we can also append integers to the end of the above list let’s say 12:
Appending list to another list with Python append method
We can also append a list to another list, we can do this by simply creating the list that we wish to append inside the append function. For example, we can append a list containing the word tiger and an integer 12 to the end of the list ‘mylist’.
Appending a list to another list creates the appended list inside the original list as shown above. This also works for dictionaries, so we can append a dictionary to our list. This can be done by simply creating a key-value pair(s) of the dictionary that we intend to append to the end of the list inside the append method.
We can also loop through something, say an iterable and then add the elements to the elements to the original list. We are going to create an empty list and then create a for loop that loops through a range of numbers and appends them to the empty list that we have created.
The above loop assigns x to the next number in the range every time it loops through the range, the statement mylist.append(x) adds the value of x to the empty list creating a list of numbers that do not include 10.
More often than not we might be working with long lists of dictionaries or tuples and this function is quite useful in such scenarios.
Appending to a file with Python append method
Using the context manager we can also append things to a file. In this case, we can call the text file ‘mytextfile.txt’ and since the file does not exist yet the context manager will create and open it for us. To open a file using the context manager we will first begin with the keywords ‘with open()’, and then pass in the name of the file that we intend to open which in this case is a text file by name ‘mytextfile.txt’.
We would then go ahead to specify the mode that we wish to open the file with and in this case we are going to open it as ‘a’. Since we are going to store it as a variable we would then add ‘as f’ at the end of the above statement.
In the subsequent line within the context manager, we will specify the text that we want to be appended to the file inside the parentheses that precede the command ‘f.write()’ as shown here:
Now if we wish to store data to the same file without overwriting the entire file every time we want to add new data, then in such a case we would use ‘a’ as opposed to the reading and write command.
In the section of code above, there are no spaces left between the texts every time that we append to the file. If we wish to append the text to a new line every time we append to the file then we would simply use a newline escape character at the end of the text that we wish to append to the file ‘mytextfile.txt’.
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- Best way of using the Python for loop
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
- Best way of using Python Split
- Best way of using Python Try-Except
- Best way of using Python Tuple
- Best way of using Python Arrays
- Best way of using Python Sort
- Best way of using Python DateTime
- How to download Python?
- Best way of using Python FileWrite
- Best way of using Python Lambda
- Best way of using Python ListAppend
- Best way of using Python ListComprehension
- Best way of using Python Map
- Best way of using Python Operators
- Best way of using Python Pandas
- Best way of using Python Requests
- Best way of using Python Strings
- Best way of using Python Count
- Best way of using Python Comments
- Best way of using the Python File Reader method
- Best Python IDE-s to use for developing Python codes
- Best way of using Python logging
- Best way of using Python Print
- Best way of using Python Zip
- Best way of using Python Append
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.