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 7530217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:51:02+00:00 2026-05-30T04:51:02+00:00

For example, in the code below I would like to obtain the list [1,2,3]

  • 0

For example, in the code below I would like to obtain the list [1,2,3] using x as a reference.

In[1]: pasta=[1,2,3]
In:[2]: pasta
Out[2]: [1, 2, 3]
In [3]: x='pas'+'ta'
In [4]: x
Out[4]: 'pasta'
  • 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-05-30T04:51:04+00:00Added an answer on May 30, 2026 at 4:51 am

    What you are trying to do is a bad practice.

    What you really need is a dict:

    >>> dct = {'pasta': [1,2,3]}
    >>> x = 'pas' + 'ta'
    >>> dct[x]
    [1, 2, 3]
    

    This is the right data structure for the actual task you’re trying to achieve: using a string to access an object.

    Other answers suggested (or just showed with a worning) different ways to do that. Since Python is a very flexible language, you can almost always found such different ways to follow for a given task, but “there should be one– and preferably only one –obvious way to do it”[1].

    All of them will do the work, but not without downsides:

    • locals() is less readable, needlessly complex and also open to risks in some cases (see Mark Byers answer). If you use locals() you are going to mix the real variables with the database ones, it’s messy.
    • eval() is plain ugly, is a “quick-and-dirty way to get some source code dynamically”[2] and a bad practice.

    When in doubt about the right way to choose, tring to follow the Zen of Python might be a start.

    And hey, even the InteractiveInterpreter could be used to access an object using a string, but that doesn’t mean I’m going to.

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

Sidebar

Related Questions

In my example code below i would like to replace the item in the
I have adapted the state example (code below). What I would like to know
In the example below, if client code using GetPeople wanted to print the name
I constantly find myself writing similar code like the example below: if (object[Object Name]
For example I have code below string txt=I have strings like West, and West;
I have the code below and I get the result like this C:\\Users\\Administrator\\Projects\\CA\\Libraries\\ConvertApi-DotNet\\Example\\word2pdf-console\\bin\\Release\\\\..\\..\\..\\..\\test-files\\test.docx The
I would like to implement a bidirectional fifo. The code below is functioning but
How would you configure annotations in the following example code? I'd like to stick
In the example code below, I have a question about the List. My prof
The example code below works as as a server process. But when I add

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.