Add Elements To An Array In Python

Arrays are represented in Python using a list. Lists function similarly to arrays in other programming languages, and this tutorial will teach you how to add elements to an array in Python.

To add elements to an array, you can use the list, NumPy array, and arrays (explicit library in Python) and then use the respective function such as insert(), append(), and extend().

In this example, I’ll show you how to add elements to an array in Python using the List Method.

Adding items to Array Using List

As you may know, lists in Python are similar to arrays. As a result, in Python, you have the following functions to add any elements to it.

  • append() function: It extends the array by inserting elements at the end.
  • insert() function: At the given index, it inserts the specified elements.
  • extend() function: Appending items from both lists makes the list longer.

Let’s take a look at an example of the code for the above function, which can be used to add elements to an array in Python.

Example 1: Using Append() Function To add Elements at End Of The Array

As you can see in the below example code, I have initialized a list with no elements and then I have added a number to the list and printed the list to see what elements it has.

#Initializing List as Array
newArray = []

#Using Append Function Adding numbers to list
newArray.append(1)
newArray.append(2)

#Printing The List to See If Numbers are added
print("The Elements of List are {}".format(newArray))

Output:

The Elements of List are [1, 2]
Python Add Elements To An Array

Example 2: Using insert() Function To Add Elements at Specific Location In Array

Continuing from the previous example, suppose you want to add number 3 to the array at location 1. In this case, the insert() function can be used. Because this function allows you to add elements to the array at a specific location.

#Initializing List as Array
newArray = []

#Using Append Function Adding numbers to list
newArray.append(1)
newArray.append(2)

#Using Insert Function To Add 3 at Location 1 in Array
newArray.insert(1, 3)

#Printing The List to See If Numbers are added
print("The Elements of List are {}".format(newArray))

Output:

The Elements of List are [1, 3, 2]
Python Add Elements To An Array

Example 3: Using extend() function To Extend the Array to a certain Length

As an extension of the preceding example, if you want to add a list of elements to an array at the same time, you can do so by calling the extend() function in Python.

As an example, let’s take a look at the code below, which shows how to use the extend() function to add a list of elements to an array in both directions in Python.

#Initializing List as Array
newArray = []

#Using Append Function Adding numbers to list
newArray.append(1)
newArray.append(2)

#Using Insert Function To Add 3 at Location 1 in Array
newArray.insert(1, 3)

newArray.extend(["You", "Are", "On", "Coduber"])

#Printing The List to See If Numbers are added
print("The Elements of Array are {}".format(newArray))

Output:

The Elements of Array are [1, 3, 2, 'You', 'Are', 'On', 'Coduber']

As shown in the preceding example, I was able to extend my array by adding elements of a new array with strings to my existing array using Python’s extend function.

Python Add Elements To An Array

Wrap Up

I hope you now understand how to add elements to an array in Python using the list class’s append(), insert(), and extend() functions. Because the list in Python functions as an array.

To add elements, you can use any of the three methods listed. You should not use an external library to represent an array in Python because a list behaves similarly and has many more features than a normal array.

If you know of a better method than the one discussed above, please let me know in the comments section and I will gladly add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Take Integer Input In Python 3
  2. 3 Ways To Check If An Array Is Empty In Python
  3. How To Compare String and Integer in Python
  4. How To Print Fractional Part In Python
  5. [Fix] SyntaxError: unexpected EOF while parsing in Python

Leave a Comment