Python is often considered one of the most beginner-friendly programming languages due to its clear syntax, readability, and vast community support.
Python is one of the most popular programming languages. It’s used in many different fields like web development, data science, machine learning, and more. To be able to program in Python, one of the prerequisites is an understanding of its rich data structure. In this blog, we explore how the data type List
plays the role of an array. We also talk briefly about two alternatives to List
, the standard Python library module array
and the package numpy
.
We’ll cover:
Get hands-on with Python for free.
Learn the fundamentals of Python with Educative’s 1-week free trial.
An array is a data structure that’s used across many programming languages for storing a collection of data items in contiguous memory locations. All the data items present in an array have the same data type. The numbered location of an element in the array is called its index.
A major advantage of storing data in contiguous locations is the ability to do a fast retrieval of the data elements using an index. A frequently asked question by beginners is how to implement a Python array. In Python, the data type List
serves the same purpose as an array, but it is richer in some respects.
A Python List
can be characterized as follows:
Note: A Python
List
is not a linked list, and allows fast retrieval (in constant time) of any data element.
dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]print(dogs)
dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]dogs.append("Wrigley")print(dogs)
We can use the insert()
method to add an element to a given index location within our list. Here’s an example:
dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]dogs.insert(1, "Wrigley")print(dogs)
We can access a list item by referring to its index. In Python, the first element is indexed by the number . For example, if we want to get the value of the first item, we can use the following code:
dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]x = dogs[0]print(x)
If we want to change an element of a list, we use its index to modify the value. For example:
dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]dogs[0] = "Jack"print(dogs)
There are a couple of different ways to remove elements of a list. We can use the pop()
Python function to remove an element at a specified position. Let’s say we want to remove the second element of our dogs
list:
dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]dogs.pop(1)print(dogs)
We can use the remove()
method to remove a specified element from a list. Here’s an example:
dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]dogs.remove("Duke")print(dogs)
We can use the len()
method to return the length of a list. If we want to return the number of elements in the dogs
list, we can use the following code:
dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]x = len(dogs)print(x)
Get started with Python fundamentals for free with our 1-week Educative Unlimited Trial. Educative’s text-based learning paths are easy to skim and feature live coding environments, making learning quick and efficient.
We can use the sort()
method to sort our list in ascending or descending order. If we want to sort our list in ascending order, we can use the following code:
numbers = [5, 13, 25, 2, 98, 56, 4, 8]numbers.sort()print(numbers)
To sort our list in descending order, we can use the following code:
numbers = [5, 13, 25, 2, 98, 56, 4, 8]numbers.sort(reverse=True)print(numbers)
We can use the count()
method to return the number of elements with the specified value. For example, let’s say we want to return the number of times the value “Spot” appears in our dogs
listarray:
dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]x = dogs.count("Spot")print(x)
A 2D array, or a two-dimensional array, is used for storing tabular data which is organized in rows and columns. A 2D array in Python can be implemented by creating a list that contains other lists that represent rows. An element in the 2D array implementation can be accessed using two indices.
Let’s move on to an example. Imagine we need to keep track of how the temperature changes throughout the day. We’ll take four temperatures: one in the early morning, one in the late morning, one in the afternoon, and one in the evening. We can store these temperatures using lists to mimic a 2D array.
Day one | Day two | Day three | Day four |
52 | 50 | 53 | 51 |
60 | 58 | 61 | 59 |
66 | 62 | 67 | 65 |
63 | 60 | 64 | 62 |
temperatures = [[52, 60, 66, 63], [50, 58, 62, 60], [53, 61, 67,64], [51, 59, 65, 62]]print(temperatures[0][1])
We can perform the same actions on these 2D data structures as we can on standard lists.
There are other ways to implement an array in Python.
array
, which can be imported and used for creating an array. The arrays created in this manner are more efficient than lists, but they do not provide support for nonnumeric data, and they can only store objects of a single data type. In the example below, we create an array of integers and then remove one of the elements.import array as arrtemp = arr.array('i', [55, 20])temp.remove(55)print(temp[0])
NumPy
is heavily used by the data science community. NumPy
array elements are also constrained to contain elements of a single data type. But these arrays support a rich set of features. The following code shows how to create a NumPy
array. It also shows a concise syntax that can be used for multiplying all entries of the array by 5
. There are many such features that make this library a great option when dealing with large sets of data.import numpy as npnumbers = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])print(numbers)multiples = numbers * 5print(multiples)
If you're looking to grow your career in machine learning or data science in this day and age, adding a powerful library to your skill set is an important place to start. In that vein, Python has become one of the most widely used tools in the industry for serious data analytics, and NumPy is probably the most widely used data analytics library. With NumPy, you can manipulate data involving multi-dimensional arrays and matrices (think linear algebra). Join us as we venture into the vast world of NumPy in this comprehensive course. Each lesson dive into the actual implementation of concepts in both pure Python and then NumPy, exploring how NumPy vectorization compares to traditional Python that uses a procedural and object-oriented approach. Practice and test yourself along the way with in-browser coding challenges, quizzes, and more. This course is intended for users who are already familiar with intermediate level Python.
Congrats on taking your first steps with Python! To recap, we addressed a common source of confusion for individuals looking to learn more about Python arrays. We learned that lists are the data structure of choice for implementing arrays in Python, with many advantages over traditional arrays. We also learned how the module array
in the standard Python library can be used to implement a Python array. For special heavy duty work, we can also use the powerful third party library Numpy
.
There’s still a lot more to learn about the Python programming language. Some recommended concepts to cover next include:
To get started learning these concepts and more, check out Educative’s learning path Python for Programmers. In this path, you’ll start by learning the fundamentals of Python, and then move into more advanced concepts including modules and web-related tasks. By the end, you’ll have the advanced knowledge to confidently use Python in your next project.
Happy learning!
Free Resources