Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.python Subject: Re: If a dictionary key has a Python list as its value! Date: 7 Mar 2024 19:21:31 GMT Organization: Stefan Ram Lines: 20 Expires: 1 Feb 2025 11:59:58 GMT Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de Q7ZLnUGTc96YZRnDIpZBiw2xsCapSKaha/pUmSDyitkBEa Cancel-Lock: sha1:PxkCyepN65ovkGew1+8tM+y4rOY= sha256:2O96c0qj8vhSPpbWVEuIuRNNn0FQ7hrcm5/JHjHBKxk= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en-US Accept-Language: de-DE-1901, en-US, it, fr-FR Bytes: 2207 Varuna Seneviratna wrote or quoted: >If a dictionary key has a Python list as its value, you can read the values >one by one in the list using a for-loop like in the following. >d = {k: [1,2,3]} >> for v in d[k]: >> print(v) >No tutorial describes this, why? >What is the Python explanation for this behaviour? This is explained by extensionality: To find the behavior of "for v in ...", the only thing one needs to know about "..." is its value. You could just as well have written: l = d[ k ] for v in l: . l can be any iterable. It does not matter where it came from. It does not matter that it cam from a dictionary. There are thousand places where it could have come from, and no tutorial can name them all.