Python

The Whacky World of Python Lists

December 10, 2022

Edward_Avedisian-Reverberations

So, Lists in Python are one of those things that at first glance appear easy but grow much more nuanced the more you try to do with them.

For many of these examples, I'm using the Python Shell. The “>>>” is the shell prompt, the unannotated lines below that are the response from the shell. These samples used Python 3.9 and 3.10, both from the Anaconda distribution for Windows.

In Python, as in many other languages, a list is a compound data type, meaning it groups together multiple values under a single variable name:

Make a list under one variable name:
Variable list

Chop up a list:
chop a list

Strings can be split into lists:
split a string

Add to a list:
group append
group append

Replace an element in a list:
Replace element in a list

Remove items from a list (3 ways):
remove element from a list

Determine the length of a list:
length of a list

Clear list:
clear list

Sorting Lists:
list sort

Create a Multi-Dimensional List:
list sort

break

More Tech Tuts