What is the difference between list and tuples in Python?

Member

by jarod , in category: Python , 2 years ago

What is the difference between list and tuples in Python?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by kennedi , 2 years ago

@jarod 

Lists:

Lists are mutable i.e and can also be reworked.

Lists are slower than all tuples.

Syntax: list_1 = [10, ‘Pleasanton’, 20]

Tuples:

Tuples are immutable,(tuples are lists, that can't ever be edited)

Tuples are faster than all lists.

Syntax: tup_1 = (10, ‘Pleasanton’ , 20)

by reynold.dach , 10 months ago

@jarod 

In Python, lists and tuples are two different types of built-in data structures.


The main differences between lists and tuples are:

  1. Lists are mutable whereas tuples are immutable. This means that once a tuple is created, it cannot be modified; whereas a list can be modified by adding, removing or changing elements.
  2. Lists are defined using square brackets [], whereas tuples are defined using parentheses () or simply by separating values with commas.
  3. Lists are used to store collections of elements of arbitrary types, whereas tuples are generally used to store structured data (such as records or database rows) or to group homogeneous elements.


Here is an example of a list and tuple:

1
2
3
4
5
# List example
my_list = [1, 2, 'apple', True]

# Tuple example
my_tuple = (3, 4, 'banana', False)