Examples of Using Python Data Structures, Part 1: List

Some useful examples of using Python List

Kantajit Shaw
Geek Culture
4 min readMay 30, 2021

--

Photo by Adeolu Eletu on Unsplash

Python has four kinds of in-built data structures i.e. List, Dictionary, Set & Tuple. In this article, we will get to explore python lists.

Let's see some common but useful examples using python lists. All these examples are written in Python 3.

List

List indexing

Python list indexing is Zero-based indexing. In which 1st element is indexed as 0, the second element is indexed as 1 as so on…

Python also supports Negative-indexing. -1 gives you the last element of the list, -2 gives you the second element from last.

List slicing

Python list can be sliced by mentioning index.

list_name[<start_index>:<end_index>:<interval>] where the list will be sliced from start_index until end_index (not inclusive) jumping intervalsteps. Any of the three (start_index,end_index,interval) can be blank. If start_index is missing it will be set to starting index of the list. Similarly if end_index is missing it will be set to the last index of the list. Let’s see some examples.

You can create empty lists using one of the following two ways.

List comprehension

We can use list comprehension to generate a list from another list. It is a shorter way. In the next snippet, we use a loop to generate a list that contains the first character of the a_list. It also allows python to decide what is the best way ( sequentially or parallelly ) to implement.

But if we use list comprehension, then the code will look like this

List operations

We can perform multiple operations on a list. The most common of them are append, extend, remove, pop, etc. We will see them in action in the following code snippets.

We can append one item at the end of the list using the append method.

We can append multiple elements of one list in another list

Note: that if we try to append a list using the append method, the whole list is considered as an item and inserted in the list.

We can remove items from the list using the pop method. Without any argument, it will remove the last element and return the element. We can also remove an item by passing the item index as the argument of the pop method

An element can be also be inserted in an array by mentioning the index using the insert method

We can get the number of occurrence of an item in the list

We can remove the element also using the remove method. In case of multiple occurrences, it will delete the item it first encounters.

To find the existence and position of an item in a list we can use the index method of the list. If an item exists, it returns the index of the item, else it throws ValueError.

We can also invoke sort and reverse of the list.

Now if we want to copy a list to another list. We can simply do that using assignment operator ( = ). But it will cause some issues which we will see in the next example.

We can get around that using the copy method.

The assignment operator creates a shallow copy where the second list points to the original list. Whereas the copy method creates a whole list containing the elements of the original list.

We can clear the list by using the clear method.

Other operations on List

Fetching the length of the list.

Fetching the maximum and minimum of the list.

We can also sort the list. It doesn’t affect the original list.

That’s some basic example using the list and its methods.

Hope you like it. Stay tuned for more.

--

--

Kantajit Shaw
Geek Culture

Deep learning enthusiast, interested in Computer Vision and Natural Language Processing problems.