AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Python lists: explanation with practical examples

PARTAGEZ

Python lists help to structure content and read it later. You can modify, sort, extend or shorten your list in different ways.

Free IONOS API

Use the IONOS API at no additional cost to retrieve or update your domain, DNS and SSL data.

DNS records
SSL Administration
API Documentation

What are Python lists?

If you want to store complex information in a readable way, a Python list is the way to go. List in Python can contain simple data and object types, or can even nest different values ​​and lists. The python list can be configured in a way homogeneous or heterogeneous. Homogeneous means that it only contains data of one type. On the other hand, we speak of a heterogeneous structure if different types of files are concatenated in the list. Due to its versatility, list in Python is an optimal alternative to an array. Our article about arrays in Python explains how it works.

How are Python lists structured?

A Python list contains an indefinite number of values. They are surrounded by square brackets and are separated by commas in the Python string.

liste = [ "valeur1", "valeur2", "valeur3", "valeur4" ]

So that the list in Python can be read later, each value has an index within the list. To filter a specific value from the list, write the corresponding index number in square brackets after the Python list. The searched value is then presented to you as the output. Remember here that counting is always done from 0 in the Internet programming language. List contents can also be duplicated and split using a Python split.

Python list example

A simple example shows how to construct a Python list and read it later. For this list, we take the country names and put them in square brackets:

pays = [ "France", "Uruguay", "Allemagne", "Pays-Bas", "Ghana" ]

If you now want to remove countries that are not in Europe from this Python list, do the following:

print ( pays [ 1 ] )
print ( pays [ 4 ] )

The output then looks like this:

Sublists in Python

A Python list can also contain sublists. These are used like ordinary elements. It looks like this:

pays_capitales = [ [ "France", "Uruguay", "Allemagne", "Ghana" ], [ "Paris", "Montevideo", "Berlin", "Accra" ] ]
pays = pays_capitales [ 0 ] [ 0 ]
print ( pays )

If you want to output the capital, enter this:

capitales = [ pays_capitales [ 1 ]
villes = [ pays_capitales [ 1 ] [ 0 ]
print ( "La ville recherchée est : ", ville )

You get this as output:

La ville recherchée est : Paris

How to expand Python lists

If you want to expand your list in Python, you have three options: append, extend, and insert. We present these methods to you.

With append

With append you can extend your list by one item. It works as follows:

pays = [ "France", "Uruguay", "Allemagne", "Ghana" ]
pays.append ( "Japon" )
print ( pays )

The output is now:

[ "France", "Uruguay", "Allemagne", "Ghana", "Japon" ]

With extension

If you want to add multiple items to your Python list, use extend for this purpose.

pays «[ "France", "Uruguay", "Allemagne", "Ghana", "Japon" ]
pays.extend ( [ "Italie", "Canada", "Australie" ] )
print ( pays )

This code displays the following output:

[ "France", "Uruguay", "Allemagne", "Ghana", "Japon", "Italie", "Canada", "Australie" ]

With insert

If you want to insert a new element in the middle of your list in Python, insert is the right choice. In addition to specifying the new item, you also directly assign it an index number.

pays = [ "France", "Uruguay", "Allemagne", "Ghana" ]
pays.insert ( 1, "Chine" )
print ( pays )
[ "France", "Chine", "Uruguay", "Allemagne", "Ghana" ]

Delete items in a Python list

To remove items from your list in Python, use either remove or del.

With remove

With remove, you remove the item by enclosing it in appropriate square brackets. However, if this element occurs more than once, it is deleted only on the first occurrence.

pays = [ "France", "Uruguay", "Allemagne", "Pays-Bas", "Ghana" ]
pays.remove ( "Allemagne" )
print ( pays )

This results in the following output:

[ "France", "Uruguay", "Pays-Bas", "Ghana" ]

with led

You can also use del to remove an item from your Python list. With this method, you don’t specify the element itself, but remove it via its index.

pays = [ "France", "Uruguay", "Allemagne", "Pays-Bas", "Ghana" ]
del pays [ 2 ]
print ( pays )

The result is identical to the remove method.

Sort Python lists

The sort function allows you to organize your list with less effort. For example, if you want to sort the countries in the list alphabetically, do the following:

pays = [ "France", "Uruguay", "Allemagne", "Pays-Bas", "Ghana" ]
pays.sort ( )
print ( pays )

The output is now:

[ "Allemagne", "France", "Ghana", "Pays-Bas", "Uruguay" ]

To produce a descending order, write this information between the empty square brackets above:

pays = [ "France", "Uruguay", "Allemagne", "Pays-Bas", "Ghana" ]
pays.sort ( reverse = true )
print ( pays )

The countries are thus displayed in the order “Uruguay” to “Germany”.

Overview of methods

You can use the following methods for your Python list. You have certainly already learned about some of them in your Python tutorial.

Method

Description

append

Adds an item to the end of the list.

clear

Removes all items from the Python list.

copy

Returns a copy of your list.

count

Counts all items with a specific value in the Python list.

extend

Adds all entries to another list.

index

Finds a specific item in the Python list and returns the index number.

insert

Adds an item at a specific location.

pop

Removes an item from a specific position and uses it as the return value.

remove

Removes the first element with a specific value.

reverse

Reverses the order of the Python list.

spell

Sort the list.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Web Marketing

Overview of the main SEO tools

Andriy OrlovShutterstock Overview of the Best Google SEO Tools If you want to attract as many visitors as possible to your website, you must understand

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact