Monday, August 3, 2015

What is the difference between tuples and lists?

Tuples are fixed size in nature whereas Lists are dynamic. In other words a tuple is immutable whereas list is mutable. Hence no append,extend,remove,pop function in tuple. But you can find elements and use 'IN' operator in tuple.

Tuple is defined with putting parenthesis() around the set of values whereas list is defined using square brackets[].

>>> a_list= ['Banana', 'Apple', 'Lime']
>>> a_tuple= ('Banana', 'Apple', 'Lime')

When to use Tuples instead of List.

If you are defining a constant set of values an all you are going to do in your application is to iterate over them, You can define it as tuple instead of list because of following reasons.


1) Tuples are faster than Lists.
2)It makes your code safer if you write protect data that need not be changed.
5 Python: What is the difference between tuples and lists? Tuples are fixed size in nature whereas Lists are dynamic. In other words a tuple is immutable whereas list is mutable. Hence no append,ex...

No comments :

Post a Comment