How to use Python List: Understand with 6 examples

The List in Python

The List is among the compound datatypes available in Python. Other compound data types like Tuple and Dict are explained in their respective chapters. Among the other compound data types, the List is the most versatile.  

An example of List

A Short Video of this List Tutorial

The List literals are written within the brackets separated by commas. Python has built-in list functions that can be used to sort, append, remove, finding the length of list etc.

These functions are covered in separate chapters and links are given at the bottom of this tutorial.

Main points about List

  • Python List items (values) can be written as comma separated values in square [] brackets.

For example: list_name = []

  • Lists in Python can store different types of values or items like numbers and strings.

e.g. Py_list = [1, 2, ‘Python’, ‘lists’ ]

  • The list data type implements sequence protocol and allows to add or remove items from the list.

A few examples of lists

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

list_strings = [‘this’, ‘is’, ‘List’, ‘lesson’]

list_mixed = [1, ‘this’, 2, ‘is’, 3, ‘list’, 4, ‘chapter’]

Creating and accessing list example

In the following example, a List of numbers is created. After that, we used a for loop to display the list items by using the print function.

Python list example

See this example online

The output:

1

2

3

4

5

An example of list of strings

Following example creates a Python List of strings with four elements. After creating the list, the for loop is used to display list elements. Note that, the for loop is using the List as sequence and assigns it to the variable i.

Python list

See this example online

The Output:

this

is

list

lesson

Accessing specific item in list python example

In the following example, a List with mixed items is created. The list contains numbers and strings. The print function is used to display specific list elements by using the index number.

See this example online

The Output of above example:

this is python list chapter

List built-in methods

Python also provides useful methods of the List type that makes programmers life quite easy. A few methods are listed below with a brief along with the links to their respective chapter.

List length method

The length method of the List is used to return the total number of elements in the list. The function name is len().

An example of Python length method

See this example online

Go to List length method chapter.

List Remove method

The List remove method is used to remove elements from an existing list.

An example of Python List remove method is given below. See it by clicking the link below:

See this example online

Go to the list remove method chapter for full detail and examples.

The list Append method

The list append method is used to add an element to an existing list.

An example of the Append method

See this example online

Go to Append to list chapter for full detail

List sort method

The sort method is used to sort the list in ASC or DESC order.

For full detail go to Sort list chapter.