DEV Community

David Fagbuyiro
David Fagbuyiro

Posted on • Originally published at tealfeed.com

Understanding Array in Python 1

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:

Country1 = "Germany"



Country2 = "France"



Country3 = "Denmark"
Enter fullscreen mode Exit fullscreen mode

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:


cars = ["Lexus", "Toyota", "Mercedez"]
Enter fullscreen mode Exit fullscreen mode

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


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



cars.append("Honda")



print(cars)

Enter fullscreen mode Exit fullscreen mode

Output:


['Lexus', 'Toyota', 'Mercedez', 'Honda']
Enter fullscreen mode Exit fullscreen mode

Example 2: Using the pop() method.


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)

Enter fullscreen mode Exit fullscreen mode

Output:


['Lexus', 'Toyota']
Enter fullscreen mode Exit fullscreen mode

Example 3: Using the Remove() method.



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



cars.remove("Toyota")print(cars)
Enter fullscreen mode Exit fullscreen mode

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.



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



cars.clear()print(cars)

Enter fullscreen mode Exit fullscreen mode

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.


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



x = cars.copy()



print(x)



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



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

Enter fullscreen mode Exit fullscreen mode

Example 6: Using the Count() method.


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



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



x = cars.count("Lexus")



print(x)

Enter fullscreen mode Exit fullscreen mode

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

because "Lexus" appears twice in the cars list.

Example 7: Using the Count() method.

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



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



cars.insert(2, "Ford")



print(cars)
Enter fullscreen mode Exit fullscreen mode

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

['Lexus', 'Toyota', 'Ford', 'Mercedez', 'Lexus']
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)