How to Create An Array in Python and other things you need to know about Python Array

jenniferfu0811 - Jul 29 '21 - - Dev Community

Arrays are a powerful and useful module. In Python, there are two ways you can create an array: using the Python Standard Library or the NymPy package.

If you’re just starting out or would simply like to create an array with simple data types, the array module from the Python standard library would suffice. However, as Python is being widely used for data science, num.py array can often be seen as the de facto standard as it’s more flexible and robust.

In this tutorial, we’ll focus on using the array module in the Python standard library.

What is a Python array and what can they do?

Python arrays are data structures that can store multiple items of the same type, for example, you can have an array of strings. When you’re working with multiple items of the same data type, arrays make it easy to store, organize, remove elements, and manipulate the values.

This can sound confusing if you’re just starting out with Python arrays. So here’s another way to think about it: say that you’re organizing your bookshelf and have a list of book titles. You might want to store this list in a place you can easily access should you ever want to organize the titles, remove, or retrieve certain information. In Python, this list of titles would be stored in an array.

A common confusion you might encounter is the difference between Python array vs list. The key difference is the types of values that the two can store. While arrays can only store items of the same data type, lists can store a variation of different data types. Taking the bookshelf example from above, if your bookshelf also contains decorative items, such as potted plants, your list of bookshelf items would need to be stored in a Python list.

How to create an array in Python

Now that you know what an array is, the next step is to create a Python array. Let’s start with a simple example: to create an array in Python, you’ll need two parameters: data type and value list. Data type is the type of value that you want to store. Continuing the previous book list example, the data type here would be books, while the values would be the book titles.

Your basic syntax would look like this:

a=array(data type,value list)`
Enter fullscreen mode Exit fullscreen mode

Using the bookshelf example:

a=array(books, [Harry Potter, Game of Thrones, Narnia, The Hobbit, The Little Prince]) 
Enter fullscreen mode Exit fullscreen mode

How do you access an element in a Python array?

Each element in the array has an index number, starting with 0. This allows you to access an element in an array. The basic syntax will look like this:

Array_name[index value]
Enter fullscreen mode Exit fullscreen mode

Using the previous example, say you want to access the second element in the array.

a=array(books, [Harry Potter, Game of Thrones, Narnia, The Hobbit, The Little Prince]) 
a[1]=Game of Thrones
Enter fullscreen mode Exit fullscreen mode

How can you know a Python array length?

By understanding how the index number works, you can also figure out the Python array length. As the index number starts from 0, the Python array length will be 1 more than the last index number. Another way is to use the len() function to figure out the Python array length:

a=array(books, [Harry Potter, Game of Thrones, Narnia, The Hobbit, The Little Prince])
len(a)=5
Enter fullscreen mode Exit fullscreen mode

How to remove an element from a Python array?

There are quite a few ways to remove elements from a Python array.

If you would like to delete an element based on their index number, you can use Python's del statement:

a=array(books, [harry potter, game of thrones, narnia, the hobbit, the little prince])
del books[2]
Enter fullscreen mode Exit fullscreen mode

In the above example, you are deleting the third element from the Python array, “Narnia”. To use this method, you need to know the index number of the element you’d like to remove.

But what if you’re not sure where the element you’d like to remove is? In this case, you can use Python’s remove() method:

a=array(books, [harry potter, game of thrones, narnia, the hobbit, the little prince])
del books[harry potter]
Enter fullscreen mode Exit fullscreen mode

By using the remove() method, you’ll be able to locate and remove a specific element without needing to know its index number.

If you want to learn more about what you can do with Python arrays, you can check out this tutorial or find a senior developer to help you master Python step by step!

Python Array Python Help Python tutor.png

We covered the basics of Python arrays in this article, but this is just the tip of the iceberg. Once you’re familiar with the array module from the Python standard library, you can expand to learn more about using arrays with the NumPy package, which is often seen as the standard array function in Python and in data science nowadays.

More resources on Python arrays:

. . . . . . . . . . . . . . . .