Skip to content

Python Lists Basics

Python Lists are used to store multiple items in a single variable. They are one of a total of four data structures that are pre-installed in Python. Besides the list, they also include the tuple, the set and the dictionary.

We define a Python List by writing the elements in square brackets. We can store elements with different data types in a list. A second way to create lists is by calling the “list()” function. For this, the elements must be written in double round brackets. However, this way of writing is used rather rarely.

# Define a list of strings
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]

# List of integers
list_2 = list((1, 5, 8, 9))

# List of booleans
list_3 = [True, False, False, True]

# Mixed list
list_4 = list((True, 25, False, "hello"))

Call list items

The elements of a Python List have an index that determines the order for each element. The individual elements of a list can be called via their corresponding index. Here, of course, it must be noted that our computer starts counting at 0. So the first element of the list has the index 0.

# Call the second list element
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
print(list_1[1])

Out:
'Tokyo'

Using the index it is also possible to call the last element of the list without knowing the length of the it and thus the concrete index. With the help of the negative index we can call objects from the list starting from the back. With this counting method we start at 1 and not at 0.

# Last element of list
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
print(list_1[-1])

Out:
'Berlin'

# Second last list element
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
print(list_1[-1])

Out:
'Montreal'

If we want to query not only individual elements of the list, but a complete range, we can define it by specifying the start and end index. It should be noted that the start index is part of the output, while the end index is not.

# Returns only the second element of the list
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
print(list_1[1:2])

Out:
['Tokyo']

# Returns the second and third element of the list
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
print(list_1[1:3])

Out:
['Tokyo', 'Montreal']

It is noticeable here that the query of a list range always returns a list as a result, even if the Python List has only one element.

Change list elements

If we want to change single or multiple elements of a Python list, we call them as described above and simply redefine their value.

# Change the first element of list
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
list_1[0] = "San Francisco"
print(list_1)

Out:
['San Francisco', 'Tokyo', 'Montreal', 'Berlin']

# Change the second and third element of list
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
list_1[1:3] = ["San Francisco", "Munich"]
print(list_1)

Out:
['New York', 'San Francisco', 'Munich', 'Berlin']

At the same time, we can also insert an element anywhere in the list using the “insert()” method without changing any existing entry. The index of the following values will increase by 1 accordingly. If we simply want to append an element to the end of the list, we can do that with the “append()” method.

# Insert a value at the second place
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
list_1.insert(1, "Munich")
print(list_1)

Out:
['New York', 'Munich', 'Tokyo', 'Montreal', 'Berlin']


# Add an element at the end of the list
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
list_1.append("Munich")
print(list_1)

Out:
['New York', 'Tokyo', 'Montreal', 'Berlin', 'Munich']

Of course we can also remove values from a Python list instead of overwriting them. For this purpose there are the methods “pop()” and “remove()”. The difference between the methods is that “pop()” takes the index as input and “remove()” takes the concrete element.

# Remove the first element of the list
list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
list_1.pop(0)
print(list_1)

Out:
['Tokyo', 'Montreal', 'Berlin']

list_1 = ["New York", "Tokyo", "Montreal", "Berlin"]
list_1.remove("New York")
print(list_1)

Out:
['Tokyo', 'Montreal', 'Berlin']

Python collections

In Python, there are a total of four data types that are stored by default:

  • The list is an ordered collection of elements, which is changeable and can also contain duplicate elements.
  • The tuple is in effect a list, with the difference that it is no longer changeable. So no elements can be added or removed afterward.
  • The set does not allow duplicate entries. At the same time, the arrangement of the elements within the set is variable. The set itself can be changed, but the individual elements cannot be changed afterward.
  • Since Python version 3.7, a dictionary is an ordered collection of elements that can be changed. In the earlier versions, the dictionary is unordered.

This is what you should take with you

  • Python Lists can be used to store a collection of items in a Python variable.
  • The list is ordered and can be modified using commands.

Other Articles on the Topic of Python Lists

  • w3schools offer detailed examples of Python lists with the possibility to execute code snippets directly online.
close
Das Logo zeigt einen weißen Hintergrund den Namen "Data Basecamp" mit blauer Schrift. Im rechten unteren Eck wird eine Bergsilhouette in Blau gezeigt.

Don't miss new articles!

We do not send spam! Read everything in our Privacy Policy.

Tags:
Cookie Consent with Real Cookie Banner