Lists in Python

What are Data Structures in Python?

Data structures in Python are ways of organizing and storing data efficiently. They are used to group together a set of values. Python has four important data structures, lists, sets, tuples and dictionaries.

What are lists?

In Python, we can define a list of elements separated by commas and enclosed within square brackets. For example:

days = ['Monday', 'Tuesday', 'Wednesday', 1800, 20.357, 'Thursday', 'Friday']
print(days)

In this example, the variable days is a list.

As we can see in the same example, the individual elements that make up the list may be of different types. In this example:

  • the first three elements are strings
  • the fourth element is an integer
  • the fifth is a real number
  • and so on

In this sense, a list can be considered an “array” or an “indexed variable,” depending on the programming language.

Also note that, like strings, lists are sequences, which means they are ordered. The different elements that make up the list are always arranged in the same order. Each element can be accessed individually if we know its index in the list. As with characters in a string, indexing starts from zero, not one.

Examples:

days = ['Monday', 'Tuesday', 'Wednesday', 1800, 20.357, 'Thursday', 'Friday']
print(days[2])

days = ['Monday', 'Tuesday', 'Wednesday', 1800, 20.357, 'Thursday', 'Friday']
print(days[4])

Unlike strings, which are immutable data types, it is possible to modify individual elements of a list:

days = ['Monday', 'Tuesday', 'Wednesday', 1800, 20.357, 'Thursday', 'Friday']
print(days)
days[3] = days[3] + 47
print(days)

We can also replace some elements of the list with others:

days = ['Monday', 'Tuesday', 'Wednesday', 1800, 20.357, 'Thursday', 'Friday']
days[3] = 'July'
print(days)

The function len(), which we already used with strings, applies to lists as well, but it returns the number of elements in the list:

days = ['Monday', 'Tuesday', 'Wednesday', 1800, 20.357, 'Thursday', 'Friday']
print(len(days))

Another function allows removing an element from a list (using its index). This function is del():

days = ['Monday', 'Tuesday', 'Wednesday', 1800, 20.357, 'Thursday', 'Friday']
del(days[4])
print(days)

It is also possible to add an element to the list. To do this, we treat the list as an object and use one of its methods. The concepts of objects and methods will be explained later, but for now what matters is how it works:

days = ['Monday', 'Tuesday', 'Wednesday', 1800, 20.357, 'Thursday', 'Friday']
days.append('samedi')
print(days)

In the first line of the example above, we used the method append() to add “samedi” to the list days. The word “append” means “to add” in English. We understand that append() is a method that adds an element to the end of the list. The argument passed to this method is the element we want to add.

There are also many techniques that allow:

  • slicing a list into parts
  • inserting groups of elements
  • removing groups of elements

These techniques (which can also be applied to strings) are called slicing. They involve using multiple indices inside square brackets. For example:

days = ['Monday', 'Tuesday', 'Wednesday', 1800, 20.357, 'Thursday', 'Friday']
print(days[1:3])

These techniques will be explained in detail later.

We will later see a large number of such methods (that is, built-in functions or “wrappers” for the list type). Note that object methods are called using the dot notation:

  • variable name (object)
  • followed by a dot
  • followed by the method name
  • and parentheses

Like strings, lists will be studied in more depth later. For now, we know enough to start using them.

Example:

days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
a, b = 0, 0
while a < 25:
    a = a + 1
    b = a % 7
    print(a, days[b])

This program prints numbers from 1 to 25 and assigns each number a day of the week, repeating the 7-day cycle using the modulo operator. In the fifth line of this program, the modulo operator % is used for cycling through lists. It is represented by % in many programming languages (including Python).

No Comments

Leave a Reply