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

  • SEARCH
  • Home
  • 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 8809673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:56:07+00:00 2026-06-14T02:56:07+00:00

Suppose it is the following structure given: from django.utils.translation import ugettext_lazy as _ #

  • 0

Suppose it is the following structure given:

from django.utils.translation import ugettext_lazy as _

# Constants for all available difficulty types.
SIMPLE = 1
MEDIUM = 2
DIFFICULT = 3

# Names for all available difficulty types.
DIFFICULTIES = (
    (SIMPLE, _("simple")),
    (MEDIUM, _("medium")),
    (DIFFICULT, _("difficult")),
)

How do you get the string value, if a constant is given?

A loop is easy to program, but is there a shorter python-like way with a single expression?

The expression

DIFFICULTIES[SIMPLE][1]

returns the string “medium”. What is obviously wrong.

  • 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-14T02:56:08+00:00Added an answer on June 14, 2026 at 2:56 am

    Of course you can use a dict, but the array is given.

    So exchange it. (I’m assuming you kept the receipt..)

    >>> dict(DIFFICULTIES)
    {1: 'simple', 2: 'medium', 3: 'difficult'}
    >>> d = dict(DIFFICULTIES)
    >>> d[MEDIUM]
    'medium'
    

    Searching through an unsorted tuple for something simply isn’t the right way to go about things. I suppose you could do

    >>> next(v for k,v in DIFFICULTIES if k == MEDIUM)
    'medium'
    

    if you wanted to avoid a for loop with a colon, but that’s a little silly.

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

Sidebar

Related Questions

Suppose the following object structure: class Super {} class SubA extends Super {} class
suppose you have the following structure: #include <windows.h> // BOOL is here. #include <stdio.h>
Suppose I have the following directory structure in Vim's NERDTree: /some/folder/ |-apples.txt |-bananas.txt |-
Suppose I have the following (desired) folder structure: *CommonProject *Project#1 ----> CommonProject(link) *Project#2 ---->
Suppose I have the following structure: typedef struct { unsigned field1 :1; unsigned field2
Suppose I have the following directory structure: workspace/ __init__.py ys_manage/ __init__.py manage.py ys_utils/ __init__.py
Suppose the following table structure: Event: id: integer start_date: datetime end_date: datetime Is there
Suppose I create a document in a MongoDB collection with the following structure and
I have the following folder structure for the templates on my django app: templates/
Suppose I have the following three tables expressing a relationship where posts are given

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.