Last Updated: Thursday 21st February 2013

Overview of Python Lists and Tuples

Two of the most commonly used built-in data types in Python are the list and the tuple.

Lists and tuples are part of the group of sequence data types—in other words, lists and tuples store one or more objects or values in a specific order. The objects stored in a list or tuple can be of any type, including the nothing type defined by the None keyword.

The big difference between lists and tuples is that lists are mutable, however tuples are immutable. Meaning once tuples are created, objects can't be added or removed, and the order cannot be changed. However, certain objects in tuples can be changed, as we'll see later.

Creating a Python List or Tuple

Creating a list or tuple is easy, here are some empty ones:

To create non-empty lists or tuples, values are separated by commas:

Note: To create a tuple with only one value, add a trailing comma to the value.

Get Python List or Tuple Values

The values in lists and tuples, as with other sequence types, are referenced by an index. This is a number, referring to the sequence of the value, that begins with 0 for the first value. Such as the following:

Using an index below zero gets the values starting from the end of the list or tuple:

Slicing Python Lists and Tuples

Multiple values can be referenced by "slicing" the list or tuple (referencing a range[start:stop]):

Assigning a Python List Value by Index

Values for lists can be assigned with indexes (as long as the index already exists), but not for tuples:

Adding to a Python List

Values can also be added to lists (and lists can be combined, or concatenated) with the '+' operator, or with the standard append method:

The Python del Keyword for Lists

Values can be removed from lists with the del keyword:

Assignment and deletion methods for lists don't work for tuples, but concatenation works:

Even though tuples are not mutable like lists, with the slicing technique and a little creativity, tuples can be manipulated like lists:

In addition, if a tuple contains a list, the mutability of the list in that tuple is preserved:

Aside from the methods described here more exist for lists and tuples, but these cover the most common list and tuple operations. And remember, lists and tuples can be nested (can contain lists and tuples, and other data types that store sets of values, such as dictionaries), and can store different types simultaneously, making them very useful.

About The Author

  • http://corepython.com wesley chun

    greetings, and thanks for this intro to using lists and tuples in Python. i wanted to point out that while there are specific use cases for using tuples, i.e., related values that are naturally grouped together and remain unaltered, a container whose contents don’t/won’t change, etc., tuples shouldn’t usually be considered by developers to manipulate application data with. their primary purpose is to send data to and receive data from functions. showing how to to “work around” tuple immutability by taking left & right slices is also non-standard practice (although a good exercise in slice manipulation). for everyday developers, the two primary and most popular data structures remain the dict and the list. i’ve also outlined some of these points in a blogpost at http://wescpy.blogspot.com/2012/05/tuples-arent-what-you-think-theyre-for.html FYI.