Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'immutable': 0.09; 'key.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '(1,': 0.16; '(2,': 0.16; '(just': 0.16; '2):': 0.16; '23,': 0.16; '3):': 0.16; 'dict': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'mapping:': 0.16; 'mapping[key]': 0.16; 'tuple': 0.16; 'typeerror:': 0.16; 'unhashable': 0.16; 'wrote:': 0.18; 'feb': 0.22; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'effort.': 0.24; 'replace': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'change,': 0.30; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; 'equality': 0.31; 'tuples': 0.31; 'file': 0.32; 'lists': 0.32; 'subject:all': 0.32; 'another': 0.32; '(most': 0.33; 'subject:time': 0.33; 'subject:the': 0.34; 'subject:with': 0.35; "can't": 0.35; 'knows': 0.35; 'something': 0.35; 'equal': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'otherwise.': 0.36; 'useful': 0.36; 'subject:?': 0.36; 'two': 0.37; 'list': 0.37; 'lists.': 0.38; 'mapping': 0.38; 'pm,': 0.38; 'recent': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'skip:u 10': 0.60; 'dangerous': 0.60; 'subject:Can': 0.60; 'save': 0.62; "you'll": 0.62; 'more': 0.64; 'between': 0.67; 'sam': 0.68; 'safe': 0.72; 'cause,': 0.91; 'lists:': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=qJEihsZIWZqZ+HqVf26mAx9EgpzfanEQMN7A34vuKaw=; b=iy3uVkd94DGe7Ai0p+GmF6zu+Pvllb/AX1kK0mS9WZh7CufaD74V1I93PfRTxYEje/ 5a4xMj7TqZtPNphDRiTq/5Br9WW4BS4CVZ3flXL4qxJSImELNNCrEiVf7Tx2c84PiCrM 36EDxP+QSnm8yxDxJXXrHE2ISmJqaXJ/jLkaBkZCzwGaUxTxHbHkz49I/gIdEwgHF8eP OCi1TS4iS2x4CyKFPcW96j5xoRay/MyPHq6pihxCN6u7qtjgqdnfztabU1gno5zpBQtU izaop9wOKxG0X+Btnj3J6AIAXVybEd2/syVUnHLS61+fjr8KWbsw0Qvl9Li1gnnEAulh BVeQ== MIME-Version: 1.0 X-Received: by 10.68.200.74 with SMTP id jq10mr17383150pbc.169.1393129107618; Sat, 22 Feb 2014 20:18:27 -0800 (PST) In-Reply-To: <64af70e3-6876-4fbf-8386-330d2f48735a@googlegroups.com> References: <64af70e3-6876-4fbf-8386-330d2f48735a@googlegroups.com> Date: Sun, 23 Feb 2014 15:18:27 +1100 Subject: Re: Can tuples be replaced with lists all the time? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393129116 news.xs4all.nl 2916 [2001:888:2000:d::a6]:59722 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66916 On Sun, Feb 23, 2014 at 3:06 PM, Sam 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 "", line 1, in 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