Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66916

Re: Can tuples be replaced with lists all the time?

References <64af70e3-6876-4fbf-8386-330d2f48735a@googlegroups.com>
Date 2014-02-23 15:18 +1100
Subject Re: Can tuples be replaced with lists all the time?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.7273.1393129116.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Feb 23, 2014 at 3:06 PM, Sam <lightaiyee@gmail.com> wrote:
> My understanding of Python tuples is that they are like immutable lists. If this is the cause, why can't we replace tuples with lists all the time (just don't reassign the lists)? Correct me if I am wrong.
>

One reason is performance/efficiency. If Python knows this is never
going to change, it can save some effort. But a more important reason
is hashability.

>>> mapping = {}
>>> key = (1,2)
>>> mapping[key] = "Hello"
>>> key = (1,3)
>>> mapping[key] = "World"
>>> key = (2,3)
>>> mapping[key] = "!"
>>> mapping
{(1, 2): 'Hello', (1, 3): 'World', (2, 3): '!'}

You can't do this with lists:

>>> key = [1,2]
>>> mapping[key]
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    mapping[key]
TypeError: unhashable type: 'list'

This is because any two tuples are either equal or not equal, they
can't be otherwise. I can create another tuple and look up something
in the above mapping:

>>> mapping[1,3]
'World'

But with lists, their equality can change.

>>> lst1 = [1,2]
>>> lst2 = [1,3]
>>> lst1 == lst2
False
>>> lst1[1] += 1
>>> lst1 == lst2
True
>>> lst1[1] += 1
>>> lst1 == lst2
False

So it would be very difficult and dangerous to try to use a list as a
dictionary key. The only safe way to do it is by identity, and that's
only useful in a very small set of situations. So Python happily
declares that a tuple can be a dict key and a list can't, and that's
now a key (if you'll excuse the pun) difference between them.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Can tuples be replaced with lists all the time? Sam <lightaiyee@gmail.com> - 2014-02-22 20:06 -0800
  Re: Can tuples be replaced with lists all the time? Paul Rubin <no.email@nospam.invalid> - 2014-02-22 20:28 -0800
  Re: Can tuples be replaced with lists all the time? Chris Angelico <rosuav@gmail.com> - 2014-02-23 15:18 +1100
  Re: Can tuples be replaced with lists all the time? Ben Finney <ben+python@benfinney.id.au> - 2014-02-23 15:49 +1100
  Re: Can tuples be replaced with lists all the time? 88888 Dihedral <dihedral88888@gmail.com> - 2014-02-23 11:45 -0800
  Re: Can tuples be replaced with lists all the time? Roy Smith <roy@panix.com> - 2014-02-22 23:19 -0500
    Re: Can tuples be replaced with lists all the time? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-03-02 17:21 -0500
  Re: Can tuples be replaced with lists all the time? Grant Edwards <invalid@invalid.invalid> - 2014-02-23 17:07 +0000
    Re: Can tuples be replaced with lists all the time? Roy Smith <roy@panix.com> - 2014-02-23 12:48 -0500
      Re: Can tuples be replaced with lists all the time? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-01 21:20 +0000
      Re: Can tuples be replaced with lists all the time? Terry Reedy <tjreedy@udel.edu> - 2014-03-01 18:15 -0500

csiph-web