Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (55.6k points)

Can anyone explain the difference between a python tuple and python list?

1 Answer

0 votes
by (119k points)

The main difference between tuple and list is mutability. Lists are mutable i.e. the list can be modified whereas a tuple is immutable i.e. tuple cannot be changed after creating.

In the following example, we can see how to override the first element of the list

a = [“apples”, “mangoes”, “oranges”]

a[0] = “berries”

a

The expected output for the above code will be:

>>>a

[“berries”, “mangoes”, “oranges”]

We will get an error stating tuple object does not support the assignment if we try to modify the first element of tuples.

b = (“apples”, “mangoes”, “oranges”)

b[0] = “berries”

b

This is one of the most frequently asked questions in python interviews. You can check out Python interview questions for more such FAQs in python interviews.

You can watch this video on Python for beginners to learn using lists ad tuples in python:

Related questions

0 votes
1 answer
0 votes
4 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...