Python | What Are Differences Between Lists And Tuples?

Sinem Yontar
Yetkin Yayın
Published in
3 min readDec 3, 2021

Lists in Python
Lists are one of the most powerful tools in python. The most powerful thing is that list need not be always homogeneous. A single list can contain strings, integers, as well as objects. Lists can also be used for implementing stacks and queues. Lists are mutable, i.e., they can be altered once declared.

The output is :

[1, 'a', 'string', 3]
[1, 'a', 'string', 3, 6]
[1, 'a', 'string', 3]
a

Here is a list of numbers.

>>> x = [1, 2, 3]

And here is a list of strings.

>>> x = ["hello", "world"]

List can be heterogeneous. Here is a list containing integers, strings and another list.

>>> x = [1, 2, "hello", "world", ["another", "list"]]

The built-in function len works for lists as well.

>>> x = [1, 2, 3]
>>> len(x)
3

The [] operator is used to access individual elements of a list.

>>> x = [1, 2, 3]
>>> x[1]
2
>>> x[1] = 4
>>> x[1]
4

The first element is indexed with 0, second with 1 and so on.

Tuples in Python
A tuple is a sequence of immutable Python objects. Tuples are just like lists with the exception that tuples cannot be changed once declared. Tuples are usually faster than lists.

example

The output is :

(1, 'a', 'string', 3)
a

Also, Tuple is a sequence type just like lists, but it is immutable. A tuple consists of a number of values separated by commas.

>>> a = (1, 2, 3)
>>> a[0]
1

The enclosing braces are optional.

>>> a = 1, 2, 3
>>> a[0]
1

The built-in function len and slicing work on tuples too.

>>> len(a)
3
>>> a[1:]
2, 3

Since parenthesis are also used for grouping, tuples with a single value are represented with an additional comma.

>>> a = (1)
>> a
1
>>> b = (1,)
>>> b
(1,)
>>> b[0]
1

Summary, lists which you created in python can able to changed whereas tuples do not be changeable (NEVER!)

--

--