Understanding Array in Python 1
Published: 2022-11-19 21:59:34

## Introduction



In this tutorial, we will be going through what an array is and possible ways to add elements to an existing array in Python. There isn't a specific data type for an array in Python. A list, which has all the attributes of an array, can be used. To create an array of integers and floating-point numbers, you have to use the Python array module.



## What is an Array?



An array is a unique type of variable that has the capacity to store several values at once. If you have a list of objects, such as a list of country names, you may store the countries in separate variables as follows:


```Python
Country1 = "Germany"



Country2 = "France"



Country3 = "Denmark"
```


But suppose you want to search through all of the countries to discover a certain one? What if you had 200 countries instead of only 3?



The alternative is an array!



An array allows you to store several values with the same name and access them by using an iterator.



## Methods in Array



You can use various built-in Python methods on lists and arrays. Below are the methods you can use on arrays and lists in Python.



- Append()



The append method adds an element at the end of the list.



- Clear()



The clear() method removes all the elements from the list, just as the name implies clear ().



- Copy()



This method returns a copy of the list.



- Count()



This method returns the number of elements with the specified value.



- Extend()



This method adds the elements of a list (or any iterable) to the end of the current list.



- Index()



This method returns the index of the first element with the specified value.



- Insert()



This array method adds an element at the specified position.



- Pop()



This method removes the element at the specified position.



- Remove()



This method removes the first item with the specified value.



- Reverse()



This method reverses the order of the list.



- Sort()



This method sorts the list, just as the name implies.



Below are some examples to show how to use some of the Python array methods:



Example 1: Using the Append () method



Create an array containing the names of cars:

```Python

cars = ["Lexus", "Toyota", "Mercedez"]
```


You can use the append() method to add a new element to an existing list/array just as seen below.

```Python

cars = ["Lexus", "Toyota", "Mercedez"]



cars.append("Honda")



print(cars)

```

Output:

```

['Lexus', 'Toyota', 'Mercedez', 'Honda']
```


Example 2: Using the pop() method.

```Python 

You can use the pop() method to remove an element from an array/list.



Delete the second element of the car array:



cars = ["Lexus", "Toyota", "Mercedez"]



cars.pop(2)print(cars)

```

Output:

```

['Lexus', 'Toyota']
```


Example 3: Using the Remove() method.
```Python


cars = ["Lexus", "Toyota", "Mercedez"]



cars.remove("Toyota")print(cars)
```


The remove() function may also be used to remove an element from an array, but it should be noted that it only removes the first instance of the specified value from a list.



Example 4: Using the Clear() method.
```Python


cars = ["Lexus", "Toyota", "Mercedez"]



cars.clear()print(cars)

```

Output:



Based on the explanation of the Clear() method above, the result of this expression will be [] empty because we have cleared the entire list.



Example 5: Using the Copy() method.

```Python

cars = ["Lexus", "Toyota", "Mercedez"]



x = cars.copy()



print(x)



#Output of the above using the copy () method will be:



['Lexus', 'Toyota', 'Mercedez']

```

Example 6: Using the Count() method.

```Python

Return the number of times the value "Lexus" appears in the car list.



cars = ["Lexus", "Toyota", "Mercedez", "Lexus"]



x = cars.count("Lexus")



print(x)

```

#Output of this would return int "2" as the result



#because "Lexus" appears twice in the cars list.



Example 7: Using the Count() method.


```Python
Insert the value "Ford" as the third element of the car list.



cars = ["Lexus", "Toyota", "Mercedez", "Lexus"]



cars.insert(2, "Ford")



print(cars)
```


#The output of the insert function in this expression will be:


```Python
['Lexus', 'Toyota', 'Ford', 'Mercedez', 'Lexus']
```


## Conclusion



Hopefully, after reading this article, you should now have a basic understanding of what an array is in Python. The basic methods used to modify an array or a list and how to use them.