Python List And Tuple Example

1. Python List

One of Python’s built-in data types is list. A list is an ordered collection of elements that can be added and removed at any time. Below is an example of python list. The variable employees is a list. You can use the len() function to get the number of list elements

>>> employees = ['Richard', 'Tom', 'Kevin']
>>> employees
['Richard', 'Tom', 'Kevin']
>>> len(employees)
3

Use an index to access each element in the list, remember that the index starts at 0. When the index is out of scope, Python reports an IndexError, so make sure the index doesn’t cross the bounds, remember that the last element’s index is len(employees) -1.

>>> employees[2]
'Kevin'
>>> employees[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range

If you want to take the last element, in addition to calculating the index position, you can also use -1 as the index to get the last element directly.

>>> employees[-1]
'Kevin'

In this way, you can get the last 2 and the last 3 element with index -2 and -3. But when you use -4 the error happened, because there are only 3 elements, the last fourth has cross the index bound.

>>> employees[-2]
'Tom'
>>> employees[-3]
'Richard'
>>> classmates[-4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range

A list is a mutable ordered object, so you can append elements to the list end.

>>> employees = ['Richard', 'Tom', 'Kevin']
>>> employees.append('Jack')
['Richard', 'Tom', 'Kevin', 'Jack']

You can also insert element into a specified location, such as a location with an index number of 1.

>>> employees = ['Richard', 'Tom', 'Kevin']
>>> employees.insert(1, 'Jack')
['Richard', 'Jack', 'Tom', 'Kevin']

To remove the element at the end of the list, use the pop() method. To remove the element at the specified location, use the pop(i) method, where i is the element index.

>>> employees = ['Richard', 'Tom', 'Kevin']
>>> employees.pop()
'Kevin'
>>> employees
['Richard', 'Tom']
>>> employees.pop(0)
>>> employees
['Tom']

To replace one element with another, you can assign a value directly to the corresponding index element.

>>> employees = ['Richard', 'Tom', 'Kevin']
>>> employees[1] = 'Jerry'
>>> employees
['Richard', 'Jerry', 'Kevin']

The data types of the elements in the list can also be different.

>>> list = ['Mac', True, 123]

The list element can also contain another list.

>>> coding_language = ['java', 'c++', ['node js', 'jquery'], 'python']
>>> len(coding_language)
4

2. Python Tuple

Another ordered list object is called tuple. Tuple is very similar to list, but tuple cannot be modified once initialized. There are no append(), insert() methods for tuple. But other get elements methods are same as list. You can use employees[0] and employees[-1] as normally, but you can’t change those element value by assign different values. Because tuples are immutable, the code is safer.

You can define a tuple use ( ), but list definition should use [ ] . When you define a tuple the elements of the tuple must be determined.

>>> t = ('Richard', 'Tom')
>>> t
('Richard', 'Tom')

If you want to define an empty tuple, you can use ().

>>> t = ()
>>> t
()

If you want to define a tuple with only one element, you should define the tuple with a comma, this is because parentheses () can represent either tuples or parentheses in a mathematical formula, this create ambiguity. If you do not add the comma python will treat it as parentheses.

>>> t = (1,)
>>> t
(1,)

If one of the tuple element is a list, then the list is changeable, let us look at an example.

>>> tup = ('R', 'T', ['J', 'K'])
>>> tup[2][0] = 'K'
>>> tup[2][1] = 'J'
>>> tup
('R', 'T', ['K', 'J'])

On the surface, the elements of the tuple has been changed, but it’s not the elements of the tuple, it’s the elements of list. The tuple doesn’t change to point to other list. So tuple’s so-called “immutable” means that every element of tuple is always point to the same object.