DEV Community

Favour George
Favour George

Posted on • Originally published at psycode.hashnode.dev on

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

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

  • 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

  • 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:

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

  • 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:

  • 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:

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

  • 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:

  • 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

These are but a few operations that can be performed on Python lists. For a more comprehensive guide visit the W3School's website.

Top comments (0)