Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a tuple is greater than a similar list? [duplicate]

Is there any reason why the next tuple is greater then the list?

>>> t = ( 1, 2, 3 )
>>> l = [ 1, 2, 3 ]
>>> t > l
True
>>> t < l
False
like image 726
sergzach Avatar asked Nov 20 '13 09:11

sergzach


People also ask

Why is a tuple better than a list?

Tuples are more memory efficient than the lists. When it comes to the time efficiency, again tuples have a slight advantage over the lists especially when lookup to a value is considered. If you have data which is not meant to be changed in the first place, you should choose tuple data type over lists.

Can a tuple have duplicate values?

Tuple is a collection which is ordered and unchangeable. Allows duplicate members.

Why tuples are faster than lists in Python?

Tuples are stored in a single block of memory. Tuples are immutable so, It doesn't require extra space to store new objects. Lists are allocated in two blocks: the fixed one with all the Python object information and a variable-sized block for the data. It is the reason creating a tuple is faster than List.

What is tuple duplication?

We consider the tuple as duplicate if all the attribute values of two rows are the same. Redundancies between attributes and duplicate tuples must be detected.


1 Answers

In Python 2, any tuple is always compared as greater than any list - that's just the way it is defined in the language.

In Python 3, this is fixed, so that comparing tuples and lists gives TypeError: unorderable types: tuple() > list().

like image 192
Daniel Roseman Avatar answered Sep 20 '22 23:09

Daniel Roseman