# Python Q: Variables Ranging Over Variables

**URL:** <https://boards.straightdope.com/t/python-q-variables-ranging-over-variables/437922>\
**Category:** Factual Questions\
**Created:** [February 16, 2008, 10:20pm UTC](https://boards.straightdope.com/t/python-q-variables-ranging-over-variables/437922 "2008-02-16T22:20:22Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [February 18, 2008, 1:16am UTC](https://boards.straightdope.com/t/python-q-variables-ranging-over-variables/437922/21 "2008-02-18T01:16:19Z")

</div>

[QUOTE=Frylock]  
btw Is there something you can do with a dictionary that you can’t do (albeit with more difficulty) with a simple and carefully formatted list?)  
[/QUOTE]

I doubt it, but lookup by key in a dictionary is likely more efficient than in such a list.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [February 18, 2008, 4:51am UTC](https://boards.straightdope.com/t/python-q-variables-ranging-over-variables/437922/22 "2008-02-18T04:51:05Z")

</div>

[QUOTE=ultrafilter]  
I doubt it, but lookup by key in a dictionary is likely more efficient than in such a list.  
[/QUOTE]  
I hate it when people trot out efficiency as the prime desiderata. The reason we use more complex data types is to make programs easier to read, reason about, and modify. Efficiency comes second to correctness, and correctness follows from readability and testing. (Metadata such as types can only catch certain kinds of problems.)

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [February 18, 2008, 4:59am UTC](https://boards.straightdope.com/t/python-q-variables-ranging-over-variables/437922/23 "2008-02-18T04:59:03Z")

</div>

[QUOTE=Derleth]  
(Metadata such as types can only catch certain kinds of problems.)  
[/QUOTE]

That’s true, but they’re very good at solving those problems. (Or, more accurately, there are a wide class of problems that types are extremely good at solving)

I strongly agree with **Derleth** ’s main point, though. If you use an abstract dictionary as opposed to a manifest list, then you cleanly separate the implementation details from the actually relevant behavior. This both makes the code using that dictionary easier to understand (being less cluttered with irrelevant low-level mucking about) and leaves you free to swap out the actual back-end implementation as you please.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [February 18, 2008, 5:11am UTC](https://boards.straightdope.com/t/python-q-variables-ranging-over-variables/437922/24 "2008-02-18T05:11:50Z")

</div>

[QUOTE=Derleth]  
I hate it when people trot out efficiency as the prime desiderata. The reason we use more complex data types is to make programs easier to read, reason about, and modify. Efficiency comes second to correctness, and correctness follows from readability and testing. (Metadata such as types can only catch certain kinds of problems.)  
[/QUOTE]

All this is true, and I agree that it’s more important to have readable code than efficient code (except in real-time systems or similar domains, where efficiency and readability are equally important). It’s been my experience, however, that no one really appreciates the importance of maintainability until they’ve been programming for a while. Efficiency, on the other hand, is something very concrete that almost every programmer appreciates, so I tend to cite it when I can depending on my audience. YMMV.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [February 19, 2008, 7:43am UTC](https://boards.straightdope.com/t/python-q-variables-ranging-over-variables/437922/25 "2008-02-19T07:43:08Z")

</div>

**ultrafilter** : OK, that makes sense. You might also emphasize that compilers are smart and high-level types and functions allow the compiler to infer things low-level types and functions don’t, leading to better-optimized code. (As a trivial example, loops created with control structures can typically be unrolled whereas loops created with goto typically aren’t.)

---

<div class="post-metadata">

**Author:** ![Bytegeist](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bytegeist](https://boards.straightdope.com/u/Bytegeist)\
**Post date:** [February 19, 2008, 3:24pm UTC](https://boards.straightdope.com/t/python-q-variables-ranging-over-variables/437922/26 "2008-02-19T15:24:22Z")

</div>

[QUOTE=Frylock]

I assume that “name in bestiary” will return “true” if name is _either_ one of the keys _or_ one of the values, right? (eta: Just did a test, and the answer is “no.” “name in dictionary” returns “true” only if name is one of the _keys_ in the dictionary.)

[/quote]

Right. If you wanted to search the values, you could do: **if name in bestiary.values()**. However, in a large dictionary, searching the values this way will be slow. (It’s a linear search.)

> [@](#):
>
> btw Is there something you can do with a dictionary that you can’t do (albeit with more difficulty) with a simple and carefully formatted list?)

As **ultrafilter** said, although you _could_ fake it with a list, it would be much less efficient. A Python dictionary guarantees that all of its keys are unique, and provides quick indexing (order log(N)) on those keys. A list doesn’t have those fancy features.

Incidentally, if you want to track a bunch of unique strings (or other objects), but don’t particularly want them mapped to anything as dictionaries always do, take a look at the **set** data type. In recent versions of Python, it’s a built-in. In earlier versions (pre 2.4), you have to import the **Set** type from the **sets** module.

[Previous page](https://boards.straightdope.com/t/python-q-variables-ranging-over-variables/437922.md?page=1)
