Python append

Table of Contents What is Python AppendPython Append to list:Python append list to another list:Python append to array: 1. Array with arrayPython append to dictionary:Python append to file: Python append to tuple:Python append to string:Conclusion: What is …

Python append

What is Python Append

The Python Append method is used to add an element or item in an immutable object at the end of the object. It does not change the complete object but only modifies it. 

You can add a single character or element, but you can add a complete list and dictionary using the append() method. The append() method does not return itself.

The Python append can be used in multiple areas. Below are all the examples where the append() method will help provide easiness to the programmers.  

Syntax: Object.append(element)

Parameter: You can add a single item as a parameter. The item can be a number, strings, list, dictionary.

This article will guide you about:

  • Python append to list
  • list to another list
  • Python append to file
  • To dictionary
  • Python append to tuple

Python Append to list:

It is used to add an item at the end of the list. The addition of a new element in the list using.

List in Python: A list is the same as an array, but what makes it powerful is all the characters or items of the list do not have the same data types all the time. However, the indexing of the list starts with 0 and goes to n-1, where n is the total number of items in the list. 

After applying the append() method on the list, no new list will return, but the previous list will be modified. The length of the index also increases by 1. 

Syntax:  List_name.append(item)

Append() methods take a single item as a parameter and add that item at the end of the list.

Example 1 Append to integer:

You can add numbers to a list using the append() method.

days = ['Monday', 'Tuesday', 'Wednesday' , 'Thursday']

days.append(3)

print(days)

Output:

Example 2 Append to string:

You can add another string using the append() method.

#append a string   
   days.append(‘Friday’)
        print(days)

Output:

Example 3 Append to Boolean:

You can add Boolean object true-false in the list by using the append method.

#append a Boolean value  
   days.append(True)
        print(days)

Output:

Python append list to another list:

If you want to merge a list inside another list, the python append() method is one of the ways. The add list will place at the end of the list to the previous list. The append method will modify the previous list.

Example

#create a new list
new_list = [2,4,6,8]

days.append(new_list)

print(days)

Python append to array: 

We are allowed to add an item or element at the end of the existing array with the help of the append method. The append to the array in python has multiple variants, and the python append to list is one.

The other variant of Python appends to the array are.

1. Array with array

Example:

#create array
   #import array for array creation
import array as arr
a = arr.array('i', [2,4,6,8])
#append an item
b = 10
a.append(b)
print(a)

Output:

2. Array to NumPy array.

Example:

import numpy as np 
x = np.arange(4) 
print("x : ", x) 
y = np.arange(20, 25) 
print("y : ", y) 
 #append 
app = np.append(x, y)
print("Final result ", app)

Output:

Python append to dictionary:

You can add an item to the key in a dictionary using the append() method.

Dictionary in Python: Dictionary is a special data type in Python that consists of a key-value pair. Each value is assigned with a key. The key can be of any data type and unique for every value. The data type of every key in a dictionary must be of the same data type.

Example: 

#create a dictionary
employee = {“name”:[], “designation”:[], “salary”:[]};
#append keys
employee[name].append(“Loren”)
employee[designation].append(“manager”)
employee[salary].append(30000)
print(employee)

In the example above, we first create a dictionary with three names, designation, and salary. All three of them are the keys, and by using the append method, we assign them values. 

Output:

Python append to file: 

File opening is one of the most used functions in Python. Python has a built-in function open(). The open() call two parameters file and mode.

The file is the absolute relative path to the file in the system. Mode basically tells python what you want to do with the file in your program. 

There are four modes:

  • Read(“r”)
  • Write(“w”)
  • Create(“x”)
  • Append(“a”)

The append in the file is again divided into two categories.

  • Append(‘a’): It opens the file for writing only, means you cannot read the other contents of the file.
  • Append(‘a+’): You are allow to read and write in the file.

So, if you want to add a new line to a file, you can open the file in either of the append modes depends on your requirement. Whatever you write using the append mode will place at the end of the existing content of the file.

Example:

Let’s take a sample file as:

Now we have to add a new line to this file. So we open the file with append mode and add the new line at its end as:

#open the sample.txt file in ‘a’ mode
file = open(‘sample.txt’ , ’a’)
#append “revised”
file.write(‘revised’)
#now close the file
file.close()

Output:

Python append to tuple:

Because a tuple is an immutable object in Python, so you cannot add or remove any item from the tuple. Therefore, the append() method will not work on tuples.

Although you can merge two tuples by concatenating them using the ‘+’ sign.

Python append to string:

The string is again an immutable object. But two strings can be concatenated with the ‘+’ operator to form a single string. 

The process of joining two strings together or string with other data types (mostly int) is also called appending. But the appending of a string with int has nothing to do with the python’s append() method. 

You can do Python string appending with the Python’s built-in function str().

Conclusion:

The python append method will allow the programmers to do a couple of things with the immutable objects.

This method will make the developer’s experience easier as it can add any of the data types in one another objects.

The append method in python will allow adding any value: a number, string, Boolean, or even a complete list to an object.

The append method in python will apply not only for the python list but also for arrays, files, dictionary, and list to another list.

Suggested Read: Remove Brackets from list Python

Leave a Comment