Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

24 Apr, 2024: Testing a new version of the Overboard here. If you have an issue post about it to rocksolid.nodes.help (I know. Everyone on Usenet has issues)


devel / comp.lang.python / Re: Why no list as dict key?

SubjectAuthor
o Re: Why no list as dict key?Chris Angelico

1
Re: Why no list as dict key?

<mailman.171.1650479335.20749.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17938&group=comp.lang.python#17938

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Why no list as dict key?
Date: Thu, 21 Apr 2022 04:28:41 +1000
Lines: 34
Message-ID: <mailman.171.1650479335.20749.python-list@python.org>
References: <CADrxXXmWEjmQmR0-mP7tugrEJ1YhqBCt_X-57fumqft4=rNPYw@mail.gmail.com>
<CAPTjJmovXqzOemVUCSoLQS--7egCj9ZQFA9DOQ9U=mgcFvUnZg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de UH3Ej8rSaS6sFF2aMVGH2AxHGOaSueccDSgQebZ8VS/A==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=qdG/SqZd;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.011
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '2022': 0.05;
'subject:Why': 0.07; 'so?': 0.09; 'subject:list': 0.11; '(1,':
0.16; '(eg': 0.16; 'chrisa': 0.16; 'dict': 0.16; 'dictionary.':
0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'janhangeer': 0.16; 'key,': 0.16; 'key:': 0.16; 'things,': 0.16;
'wrote:': 0.16; "can't": 0.17; 'thu,': 0.19; 'to:addr:python-
list': 0.20; 'list,': 0.24; 'cannot': 0.25; '>>>': 0.28;
'to:name:python': 0.32; 'message-id:@mail.gmail.com': 0.32;
'same': 0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; 'from:addr:gmail.com': 0.35; 'change': 0.36; 'using': 0.37;
"it's": 0.37; 'received:209.85': 0.37; 'could': 0.38; 'means':
0.38; 'thanks': 0.38; 'received:209': 0.39; 'list': 0.39; 'use':
0.39; 'break': 0.39; 'to:': 0.62; 'true': 0.63; "you'd": 0.64;
'key': 0.64; 'spam': 0.65; 'instead,': 0.70; 'changed,': 0.84;
'duplicate': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=eUjM6vUkZTbkrXYRpHaMbk3sZcVnlykP3Kn8Rjm0IlQ=;
b=qdG/SqZdH5IAOZKuN/75GlN87+FwvwsaRT9GXVYDRlNERB3mo0eEj6fmBPNAj9TnDg
McY+XJKtPaWhuP052UzvOR+1iP6MI+1pPLULmFEruGJJFCSKMLX8mCpYbqCn2XC1RPuc
naf+hVGHID6F/MFK03Su/BxLhCY67MExd5uNPhD1nhTrgPlRQYSAPlJkRMd99u/ZIagM
G7AJyedc+9pGZSyfiiioE7+9CDR0/nN0t3b0BA7a37yjOuxrYv90JIyDQLZ8LwvVoyUT
pjmr9mBZjznzhXPVQz9ZZCtY/TRaIHrbXF2M/1T5yFvQGj9DNBlVEV2zYRm7Jq+0YHGm
hgcQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=eUjM6vUkZTbkrXYRpHaMbk3sZcVnlykP3Kn8Rjm0IlQ=;
b=JVSS3WZ22CzPDhDCXSEUbvwSzb6xItXwWGWPxvXebzzx2lumm4jjz2xlLigWya0L3I
fpesRX29yAmAZPBdAKL5t3oYbDWaE4bfF4sa8zPB/HhP69X8hxfzUof822zOZ6fp7S4y
MMW21uec9ye3lr2ZMsR5jEQRbhS0ZX1ozD3ZGVzlaoXWz+klUDgwCPnA4YKM9mDqBeJZ
bF/R7DXHfz3laVY/vqpjeM2LNZ38irVuwnHHjrcv2X7ixDNHgpBOZ+cyjIbmhkYNbzrt
/FK4s81OvA166QpbSK5W/63QEK89ut+cniZNlchFZdJgccxGMfeWxw+DNoEuVuVZyqwi
37sA==
X-Gm-Message-State: AOAM532I8jOhw8yRxW9CdcLhh5trza20lGtAsHvO4/ein7wmtVifmfTE
RQ3VoV7O8eCohHN1b7WhOJBKc+vy7/Hr7xHSYwlCa4pr1Ss=
X-Google-Smtp-Source: ABdhPJydBfGcwEz7hqd8+AqO1n6e6BZLxsTmkACGrY/QwvzFjZo4CD4UO+SFdyE0shGRAj861dR09Cj0DoBVQnGJPAQ=
X-Received: by 2002:a7b:c201:0:b0:38f:f7f5:f6db with SMTP id
x1-20020a7bc201000000b0038ff7f5f6dbmr4904831wmi.191.1650479333282; Wed, 20
Apr 2022 11:28:53 -0700 (PDT)
In-Reply-To: <CADrxXXmWEjmQmR0-mP7tugrEJ1YhqBCt_X-57fumqft4=rNPYw@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmovXqzOemVUCSoLQS--7egCj9ZQFA9DOQ9U=mgcFvUnZg@mail.gmail.com>
X-Mailman-Original-References: <CADrxXXmWEjmQmR0-mP7tugrEJ1YhqBCt_X-57fumqft4=rNPYw@mail.gmail.com>
 by: Chris Angelico - Wed, 20 Apr 2022 18:28 UTC

On Thu, 21 Apr 2022 at 04:23, Abdur-Rahmaan Janhangeer
<arj.python@gmail.com> wrote:
>
> Greetings list,

Greetings tuple,

> Using Python3.9, i cannot assign a list [1, 2] as key
> to a dictionary. Why is that so? Thanks in advanced!
>

Because a list can be changed, which would change what it's equal to:

>>> spam = [1, 2]
>>> ham = [1, 2, 3]
>>> spam == ham
False
>>> spam.append(3)
>>> spam == ham
True

If you use spam as a dict key, then mutate it in any way, it would
break dict invariants of all kinds (eg you could also have used ham as
a key, and then you'd have duplicate keys).

Instead, use a tuple, which can't be mutated, is always equal to the
same things, and is hashable, which means it can be used as a key:

>>> spam = (1, 2)
>>> ham = (1, 2, 3)
>>> {spam: "spam", ham: "ham"}
{(1, 2): 'spam', (1, 2, 3): 'ham'}

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor