Python List

Summary: in this tutorial, you will learn about Python list and how to manipulate elements of a list effectively.

Defining a Python list

In Python, a list is an ordered collection of objects. A list can contain different types of objects, even other lists. A list is enclosed by brackets, where each element is separated by a comma.

For example, you can define a list of integers as follows:

In Python, the size of the list can grow or shrink when needed.

Let’s take a look at some examples of defining lists:

  • The list1 is a list of strings
  • The list2 is a list of strings and integers. List can have elements that have different data types
  • The list3 contains a string and another list. The list3 is called a nested list.
  • The list4 contains 2 integers and another list. The list4 is also a nested list.

The following code defines an empty list:

Python list index

You can access elements of a list via list index. List index starts from zero. For example, you can access elements of a list as follows:

List slicing

List slicing basically is another way to access list elements. You can access a sequential subset of list elements using the slice. The slice syntax as follows:

The operator (:) is used between start and end values. Both of these values are optional.

  • If you omit the start value, it returns all elements from beginning of the list to the element of the end except the last element with the end index.
  • If you omit the end value, it returns all elements starting from start index to the end element.
  • If you omit both values, you get the all elements of the list.

The following example shows you how to use list slicing:

Manipulating list elements

Modifying list elements

To modify list element, you access it via index or list slicing, and change its value, for example:

Appending the lists

Python supports two functions that allow you to append a list to another list.

Appendto append a list as an element of another list
Extendto append all element of a list to anther list

The following example demonstrates how to use use the  append() and extend() functions:

Besides the  extend() function, you can use concatenate operator (+) to append all elements of a list to another. See the following example:

Sorting the list

You can sort a list by using the built-in function: sort(). The following example shows you how to use  the sort() function to sort a list of integers:

Python allows you to define custom sorting rule by using a custom key function. Let’s take a look at an example:

Let’s examine the code above in more detail:

  • First, we defined a simple function that returns the number of characters in a string.
  • Second, we sorted the list by using default key function. The list is sorted alphabetically in ascending order.
  • Third, we pass the custom function the key parameter of the sort function. The sort function uses the custom key function for comparing objects when sorting them.

Python list supports many other useful operations listed in the table below. You can practice with each of them to see how it work.

*Use to replicated the lista = [‘A’] * 4a = [‘A’, ‘A’, ‘A’, ‘A’]
minReturns the smallest element in a listlist = [1,2,6]min(list)1
maxReturns the largest element in a listlist = [1,2,6]max(list)6
indexReturns the position of a value in a listlist = [1,2,6]list.index(2)1
countCounts the number of times a value occurs in a listlist=[1,2,2,3,2]list.count(2)3
inReturns whether an item is in a listlist = [1,2,6]a = 2
b = a in list

In this tutorial, we have introduced you to Python list and shown you how to manipulate the elements of a list effectively.

  • Was this tutorial helpful ?
  • YesNo