Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:17:33+00:00 2026-06-02T06:17:33+00:00

I like using dictionaries as a form of switch statment, by settings booleans as

  • 0

I like using dictionaries as a form of switch statment, by settings booleans as keys. Example:

>>> def f(a):
...      return {True: -a, a==0: 0, a > 0: a}[True]
... 
>>> f(-3)
3
>>> f(3)
3
>>> f(0)
0

The key True works as an else/default case and is only returned if no other key is evaluated to True. I am guessing this assumes some sort of order of evaluation for iterating the dictionary.

Now look at the following extract from the latest release announcement from the Python team for the newest versions of branches 2.6, 2.7, 3.1 and 3.2:

Hash randomization causes the iteration order of dicts and sets to be
unpredictable and differ across Python runs. Python has never
guaranteed iteration order of keys in a dict or set, and applications
are advised to never rely on it. Historically, dict iteration order
has not changed very often across releases and has always remained
consistent between successive executions of Python. Thus, some
existing applications may be relying on dict or set ordering.

Does this mean that using dicts as switch calls will not be possible anymore? Or maybe I should use any other class (like OrderedDict or something)? Or maybe I’m completely off and it should not affect this at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:17:37+00:00Added an answer on June 2, 2026 at 6:17 am

    You misunderstood how this code works.

    Your dictionary has only two keys: True and False. There could be multiple conflicting values for the True key but that gets resolved at the initialization of the dictionary.

    There’s no iteration for a dictionary lookup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for example I'm trying to get the data from database like: using (ExplorerDataContext context
I have a list of dictionaries like in this example: listofdict = [{'name': 'Foo',
I'm using a plist with structure like this: plist, array, dict key string key
I'm new to python and I really like concept of using dictionaries instead of
I am quite often using funky stuff as keys for dictionaries, and therefore, I
Like : using ::size_t; using ::fpos_t; using ::FILE; In fact it's a question inspired
I like using C-j to eval-last-sexp but paredit-mode (which I otherwise like) overrides this
I like using LINQ to SQL. The only problem is that I don't like
it seems like using $or with a sort does a full table scan and
Can you use 2 'using' statements like: using (SqlConnection ..) { using(SqlDataReader reader =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.