DEV Community

Feddy Mwanjumwa
Feddy Mwanjumwa

Posted on

Python Data Structures: Lists

When I started learning Python, I quickly realised that storing one value at a time wasn't going to be enough.

I needed a way to store multiple things together.

That's where lists came in.

At first, a list looked very simple:

products = ["Bread", "Milk", "Sugar", "Soap"]

But there is actually quite a lot you can do with a list.

Creating a list

A list is created using square brackets [].

For example:

names = ["Feddy", "Amina", "Brian"]

A list can contain numbers:

prices = [80, 120, 250, 300]

It can also contain different data types:

student = ["Feddy", 20, True, 85.5]

Although Python allows mixed data types, I usually keep a list focused on similar kinds of data because it makes the code easier to understand.

Indexing

One of the first things I learnt about lists was indexing.

Python starts counting from 0.

So:

products = ["Bread", "Milk", "Sugar", "Soap"]

print(products[0])

gives: Bread

And: print(products[2])

gives:Sugar

This was confusing at first because I naturally wanted the first item to be position 1. Eventually, I got used to Python starting at 0.

Negative indexing

Python also lets me access items from the end of the list.

print(products[-1])

gives:Soap

And: print(products[-2])

gives: Sugar

I find this useful when I want the last item without having to count how many items are in the list.

Changing items

Lists are mutable.

In simple terms, that means I can change them after creating them.

For example:

products = ["Bread", "Milk", "Sugar"]

products[1] = "Rice"

Now the list becomes:

["Bread", "Rice", "Sugar"]

This was one of the first moments where I understood that lists aren't fixed. I can keep modifying them as my program runs.

Adding items
There are a few ways to add items.

1.append()

append() adds one item to the end.

products.append("Salt")

If the list was:

["Bread", "Milk", "Sugar"]

it becomes:

["Bread", "Milk", "Sugar", "Salt"]

2. insert()

insert() lets me add an item at a specific position.

products.insert(1, "Flour")

Now Flour is placed at index 1.

3. extend()

extend() is useful when I want to add several items.

products.extend(["Tea", "Coffee"])

Instead of adding the items one at a time, Python adds both to the list.

Removing items

I can also remove items in different ways.

1. remove()

remove() removes a specific value.

products.remove("Milk")

2. pop()

pop() removes an item based on its index.

products.pop(1)

If I don't give an index, it removes the last item:

products.pop()

One useful thing about pop() is that it can give me back the item that was removed.

removed_product = products.pop()

3. del

I can also use del:

del products[0]

This removes the item at index 0.

4. clear()

And if I want to remove everything:

products.clear()

The list still exists, but it is now empty.

Checking if an item exists

I can check whether an item is inside a list using in.

For example:

products = ["Bread", "Milk", "Sugar"]

print("Milk" in products)

This gives:

True

I can also use it in a condition:

if "Milk" in products:

print("Milk is available")

This made a lot of sense to me when I started thinking about things like inventory.

Finding the length

len() tells me how many items are in a list.

products = ["Bread", "Milk", "Sugar", "Soap"]

print(len(products))

The result is:

4

This is useful when I need to know how many items I'm working with.

Slicing

Slicing lets me take part of a list instead of the whole thing.

For example:

products = ["Bread", "Milk", "Sugar", "Soap", "Salt"]

print(products[1:4])

The result is:

["Milk", "Sugar", "Soap"]

The important thing I learnt here is that the starting index is included, but the ending index is not.

I can also get the first three items:

print(products[:3])

Or everything from index 2 onward:

print(products[2:])

Sorting a list

I can sort numbers:

prices = [500, 200, 800, 300]

prices.sort()

Now I get:

[200, 300, 500, 800]

For descending order:

prices.sort(reverse=True)

I can also sort text alphabetically:

names = ["Brian", "Amina", "Feddy"]

names.sort()

The result is:

["Amina", "Brian", "Feddy"]

sorted() vs sort()

This was something I had to pay attention to.

sort() changes the original list.

sorted() creates a new sorted result.

For example:

prices = [500, 200, 800, 300]

new_prices = sorted(prices)

Now new_prices contains the sorted values, while prices remains unchanged.

Reversing a list

I can reverse a list using reverse().

products = ["Bread", "Milk", "Sugar", "Soap"]

products.reverse()

Now the order becomes:

["Soap", "Sugar", "Milk", "Bread"]

This does not sort the list. It simply reverses the current order.

Looping through a list

Lists and loops go together really well.

For example:

products = ["Bread", "Milk", "Sugar"]

for product in products:

print(product)

Python takes each item from the list one at a time.

This became one of the most useful things for me because I could perform an action on every item without writing the same code repeatedly.

enumerate()

Sometimes I want both the item and its position.

That's where enumerate() helps.

products = ["Bread", "Milk", "Sugar"]

for number, product in enumerate(products, start=1):

print(number, product)

The result is:

1 Bread

2 Milk

3 Sugar

This is useful when displaying numbered lists.

List concatenation

I can combine two lists using +.

fruits = ["Apple", "Mango"]

vegetables = ["Carrot", "Spinach"]

items = fruits + vegetables

The result is:

["Apple", "Mango", "Carrot", "Spinach"]

Repeating a list

I can also repeat a list using *.

For example:

numbers = [1, 2]

print(numbers * 3)

The result is:

[1, 2, 1, 2, 1, 2]

I don't use this as often, but it's useful to know.

Finding an item's position
If I know the value but want to find its position, I can use index().

For example:

products = ["Bread", "Milk", "Sugar"]

print(products.index("Sugar"))

The result is:

2

Counting items
count() tells me how many times a value appears.

For example:

numbers = [1, 2, 2, 3, 2, 4]

print(numbers.count(2))

The result is:

3

This is useful when duplicate values matter.

List comprehensions

Once I became comfortable with normal loops, I came across list comprehensions.

They are a shorter way of creating lists.

For example, I can create a list of squares:

numbers = [1, 2, 3, 4, 5]

squares = [number ** 2 for number in numbers]

The result is:

[1, 4, 9, 16, 25]

I wouldn't recommend trying to learn comprehensions before understanding normal loops. For me, they made more sense after I understood what the longer version was doing.

Filtering with a list comprehension

I can also use a condition.

For example, suppose I only want numbers greater than 10:

numbers = [5, 12, 8, 20, 15]

large_numbers = [number for number in numbers if number > 10]

The result is:

[12, 20, 15]

This became useful when I wanted to create a new list from existing data based on a condition.

Nested lists

A list can also contain other lists.

For example:

students = [["Feddy", 20], ["Amina", 21], ["Brian", 19]]

I can access Brian's name with:

print(students[2][0])

The result is:

Brian

Nested lists are possible, although when the data becomes more detailed, I often find dictionaries easier to work with.

Copying lists
One thing that can be confusing is copying a list.

For example:

products = ["Bread", "Milk", "Sugar"]

new_products = products

Both variables now refer to the same list.

So changing one can affect the other.

A safer way to create a separate copy is:

new_products = products.copy()

Now I can change new_products without changing the original products.

A practical inventory example

This is where lists started feeling like something I could actually use in a program.

Imagine I have a simple shop inventory:

products = ["Bread", "Milk", "Sugar", "Soap"]

I can check whether something is available:

if "Milk" in products:

print("Milk is available")

I can add a new product:

products.append("Salt")

I can remove a product:

products.remove("Soap")

And display everything:

for product in products:

print(product)

Now I'm not just learning list methods individually. I'm using them together to solve an actual problem.

What I understood about lists

The biggest thing I learnt about lists is that they are more than just a place to store multiple values.

I can add items, remove them, change them, sort them, search through them, loop through them, and even create new lists from existing ones.

The way I now think about a list is simple:

If I have a collection of items that I want to keep together and possibly change later, a list is usually a good choice.

Final thoughts

Lists were one of the first Python concepts that made me feel like I could actually build something useful.

At first I only knew how to create a list and access an item.

Then I learnt how to modify lists, loop through them, search them, sort them, and eventually use list comprehensions.

What made the difference was not memorising every method.

It was actually using lists in small programs and seeing why each feature exists.

That's when lists stopped being just another Python topic and became something I naturally reach for when I need to store a collection of things.

Top comments (0)