Sitemap
Tehreem

Tehreem

Hey everyone! I'll be documenting my cybersecurity journey here. What I learn, what I understand. Feel free to reach out and tag along :)

Python List Operations

3 min readAug 26, 2026

--

Previously, I discussed Strings and String Operations in Python. The thing about strings is that they are “immutable”, meaning we cannot replace a character in a string.

Get Tehreem’s stories in your inbox

Join Medium for free to get updates from this writer.

Lists, the topic I discuss today, however, are changeable, and they can be replaced using the index numbers.

Getting a list element using its index number
List Concatenation
Lists are mutable
Slicing and Looping through a List

1- Getting a list element using its index number:

Let’s try to access an element in a list by its index.

Since the indices in a list start from 0, the number 8 is at the 4th index.

2- List Concatenation:

Just like strings can be joined together in an output with a concatenation sign, +, so can lists.

This results in a third new list containing the elements of the first and the second list.

3- Lists are mutable:

Updating elements of a list:

As I said, lists can be changed. When we want to add something or remove some element from a list, we can do so by understanding the following example:

Just by using the index number of the element we wanted to replace and assigning a new value to it, we change the list.

Adding elements to a list:

We add elements to a list using the .insert() method, which takes two arguments: index and the element value.

Removing elements in a list:

We remove elements using the .remove() method.

But the question arises: what if there are two or more similar elements in the list, and we want to remove only the first one since it does not take an index as an argument? Let’s test what it does:

Turns out, only the first occurrence of that element is removed from the list.

Adding elements to the END of the list:

We use the .append() method to add an element to the end of a list.

The number 5 has been added to the list :)

4- Looping through a List and string slicing:

Let’s say we have a problem: there is a list of IP addresses, and we want to get only the first three digits of the IP address.

But this is fine for only one IP address; what if we have to do this repeatedly for more than one? We use loops for that and the append method to keep adding them to a newer list.

Press enter or click to view image in full size

This was all about list operations.

--

--

Tehreem
Tehreem