Skip to main content
Lists and tuples


Other important sequence types used in Python include lists and tuples. A sequence type is formed
by putting together some other types in a sequence. Here is how we form lists and tuples:
>>> [1 ,3 ,4 ,1 ,6]
[1 , 3 , 4 , 1 , 6]
>>> type ( [1 ,3 ,4 ,1 ,6] )
< type ’ list ’ >
>>> (1 ,3 ,2)
(1 , 3 , 2)
>>> type ( (1 ,3 ,2) )
< type ’ tuple ’ >
Notice that lists are enclosed in square brackets while tuples are enclosed in parentheses. Also note
that lists and tuples do not need to be homogeneous; that is, the components can be of different
types:
>>> [1 ,2 , " Hello " ,(1 ,2)]
[1 , 2 , ’ Hello ’ , (1 , 2)]
Here we created a list containing four components: two integers, a string, and a tuple. Note that
components of lists may be other lists, and so on:
>>> [1 , 2 , [1 ,2] , [1 ,[1 ,2]] , 5]
[1 , 2 , [1 , 2] , [1 , [1 , 2]] , 5]
By nesting lists within lists in this way, we can build up complicated stuctures.
Sequence types such as lists, tuples, and strings are always ordered, as opposed to a set in mathe-
matics, which is always unordered. Also, repetition is allowed in a sequence, but not in a set.

Comments