Python Tutorials · Python DSA

Lists and Arrays

Learn all about Lists and Arrays in this comprehensive tutorial.

5 min read intermediate
  • In Python, lists are the built-in data structure that serves as a dynamic array.
  • A list is a built-in data structure in Python, used to store multiple elements.
  • Lists are created using square brackets []:
  • Python lists come with several built-in algorithms (called methods), to perform common operations like appending, sorting, and more.
  • Sometimes we want to perform actions that are not built into Python.
  • When exploring algorithms, we often look at how much time an algorithm takes to run relative to the size of the data set.

Introduction

In Python, lists are the built-in data structure that serves as a dynamic array.

Lists are ordered, mutable, and can contain elements of different types.

Lists

A list is a built-in data structure in Python, used to store multiple elements.

Lists are used by many algorithms.

Creating Lists

Lists are created using square brackets []:

python

List Methods

Python lists come with several built-in algorithms (called methods), to perform common operations like appending, sorting, and more.

python

Create Algorithms

Sometimes we want to perform actions that are not built into Python.

Then we can create our own algorithms.

For example, an algorithm can be used to find the lowest value in a list, like in the example below:

python

The algorithm above is very simple, and fast enough for small data sets, but if the data is big enough, any algorithm will take time to run.

This is where optimization comes in.

Optimization is an important part of algorithm development, and of course, an important part of DSA programming.

Time Complexity

Run Time

When exploring algorithms, we often look at how much time an algorithm takes to run relative to the size of the data set.

In the example above, the time the algorithm needs to run is proportional, or linear, to the size of the data set. This is because the algorithm must visit every array element one time to find the lowest value. The loop must run 5 times since there are 5 values in the array. And if the array had 1000 values, the loop would have to run 1000 times.

Try the simulation below to see this relationship between the number of compare operations needed to find the lowest value, and the size of the array.

See this page for a more thorough explanation of what time complexity is.

Each algorithm in this tutorial will be presented together with its time complexity.

Module quiz

2 questions
1

Which of the following is true about Lists and Arrays?

2

What is the most common pitfall when working with Lists and Arrays?

Answer all questions to submit.