Home/Blog/Programming/Python in 5 minutes: Get started with Python lists
Home/Blog/Programming/Python in 5 minutes: Get started with Python lists

Python in 5 minutes: Get started with Python lists

Erin Schaffer
Oct 23, 2023
6 min read

Become a Software Engineer in Months, Not Years

From your first line of code, to your first day on the job — Educative has you covered. Join 2M+ developers learning in-demand programming skills.

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.

Python for Programmers


How to implement an array in Python?#

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:

  1. The elements stored in a list need not all have the same data type. This means that the first element of the list can be a string, the second element can be an integer, and the third can be a list of strings, etc. These elements don’t need to be unique.
  2. The order of the elements in a list is important.
  3. Lists are mutable. This means that a list can be modified by additions, updates and deletions.

Note: A Python List is not a linked list, and allows fast retrieval (in constant time) of any data element.

Create Python list#

Let’s take a look at an example of how to create a Python list:

dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]
print(dogs)

Now, let’s take a look at the different operations we can perform on Python lists.

Common list operations#

Add elements#

There are a couple of different ways to add elements to a list. We can append elements to the end of the list using the append() method:

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)

Access elements#

We can access a list item by referring to its index. In Python, the first element is indexed by the number 00. 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)

Remove elements#

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)

Find length of a list#

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)

Keep learning Python for free.#

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.

Python for Programmers


Sort Python list#

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)

Count elements#

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)

Implementing 2D arrays in Python#

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.

Other ways to implement an array in Python#

There are other ways to implement an array in Python.

  1. The Python standard library comes with a module called 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 arr
temp = arr.array('i', [55, 20])
temp.remove(55)
print(temp[0])
  1. A third party Python library called 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 np
numbers = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(numbers)
multiples = numbers * 5
print(multiples)

Cover
From Python to Numpy

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.

5hrs
Intermediate
81 Playgrounds
17 Quizzes

Wrapping up and next steps#

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:

  • Python dictionaries
  • Python tuples
  • Python strings
  • Python syntax

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!

Continue learning about Python#

Frequently Asked Questions

How difficult is Python for beginners?

Python is often considered one of the most beginner-friendly programming languages due to its clear syntax, readability, and vast community support.


  

Free Resources