How to Work with Python Lists
Published: 2023-09-02 21:56:00

---
title: How to Work with Python Lists
published: true
date: 2023-07-15 20:31:05 UTC
tags: 
canonical_url: https://psycode.hashnode.dev/how-to-work-with-python-lists
---

Lists are one of the basic data types in programming that allow programmers to represent a finite number of items. In this article, we'll discuss their importance and how to use them effectively in the Python programming language.

## Table Of Content:

- What are lists?

- Uses of Lists

- Basic Operations on Lists

**Prerequisites** : Knowledge of the Python programming language is crucial.

# What are Lists?

In computer science, a list or sequence is an abstract data type that represents a finite number of ordered values, where the same value may occur more than once. -[Wikipedia](https://en.m.wikipedia.org/wiki/List_(abstract_data_type))

Lists aid programmers to represent a finite number of items of the same data type together.

# Uses of Lists

- **Storing and Organizing Data:** Lists are used to store data of the same data type together as a collection. With the aid of lists, one can store items of the same type together such as strings, integers and objects.

- **Storing Temporary Data:** Lists are useful for storing data that isn't going to be needed for future use but is essential for calculations at the moment.

- **Iteration over items:** If the need comes to iterate over items, a list is a great way to store such items as lists are iterable.

# Basic Operations on Lists

These are some of the basic operations done on lists in Python

- 
 Creating a List

Creating a list isn't as hard as one would expect.

The code below creates a list of fruits

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689450556406/d76665ed-dd67-4157-b08c-daa4830a8d05.png)

- 
**Accessing Items in a List**

To access items on a list, one has to specify the index of the item in the list. Indexes work as numbers to show the position of an item in a list, exactly as it works with lists in real life. Take the list below for example:

- Apple

- Banana

- Cherry

The item at index 1 or number 1 is Apple. It works almost the same way in programming except that we start counting from 0 instead of 1. So referring to the same list above programmatically, the item at index 1 or number 1 is Banana while Apple is the item at index 0. Let's show that with code as seen below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689451159103/0e950cca-0a59-4ed0-80b7-0206251ea367.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689451174253/b87bac9f-dc82-475b-b66e-1127e6d87313.png)

- 
Changing Items of a List

To change an item in a list, we simply access the index of the particular item we wish to change and assign it a different value as shown below:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689451333004/8dd5a71a-de21-4133-8ac4-9dc8418098da.png)

In the code above we changed the value of the fruit at index 1 from banana to orange and this is reflected in the output below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689450845025/835b2139-1433-4da3-94e3-44bbe6a274cc.png)

- 
Removing Items from a List

To remove an item from a list we use the remove() method and specify within the method the particular item we wish to remove from the list as shown below:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689450884822/8bdf8f64-1098-40d2-8bbf-82a2785528ab.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689450904450/78106121-314d-4f44-9613-246b7ddff1c7.png)

- 
Adding Items to a List

To add a new item to a list, we simply use the append() method that lists have in Python and inside the method we provide the item we wish to add to the list as shown below:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689450941650/668648ea-b9fc-472f-9fba-6851ae3d0848.png)

It's important to note that the append method adds an item to the end of the list.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689450966936/035d05f7-de59-41a0-8378-6508691fd40e.png)

- 
Sorting a List

We can sort lists by using the sort() method on the list we intend to sort. This method will sort the list in ascending order by default, but if we set the reverse keyword argument to True, we can sort a list in descending order as well. Let's see the code for this below:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689451006797/9960b10d-9295-4123-bc6b-c8c7e6cb215f.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689451026645/e164b3dd-c6d6-449e-8d93-a23fb0d17f31.png)

- 
**Clearing a List**

To clear a list means to wipe it of all its contents. To do this we'll use the clear() method on the list we want to clear as shown below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689451060550/6d8d8627-7bde-42a7-93c4-16e84b344efb.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689451095276/aa1052ea-9c26-453c-9600-6d52d063a376.png)

These are but a few operations that can be performed on Python lists. For a more comprehensive guide visit the [W3School's](https://www.w3schools.com/python/python_lists.asp) website.