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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:14:55+00:00 2026-06-05T23:14:55+00:00

Is it possible to create a variable name based on the value of a

  • 0

Is it possible to create a variable name based on the value of a string?

I have a script that will read a file for blocks of information and store them in a dictionary. Each block’s dictionary will then be appended to a ‘master’ dictionary. The number of blocks of information in a file will vary and uses the word ‘done’ to indicate the end of a block.

I want to do something like this:

master={}
block=0
for lines in file:
  if line != "done":
    $block.append(line)
  elif line == "done":
    master['$block'].append($block)
    block = block + 1

If a file had content like so:

eggs
done
bacon
done
ham
cheese
done

The result would be a dictionary with 3 lists:

master = {'0': ["eggs"], '1': ["bacon"], '2': ["ham", "cheese"]}

How could this be accomplished?

  • 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-05T23:14:58+00:00Added an answer on June 5, 2026 at 11:14 pm

    I would actually suggest you to use a list instead. Is there any specific point why would you need dicts that are array-ish?

    In case you could do with an array, you can use this:

    with open("yourFile") as fd:
        arr = [x.strip().split() for x in fd.read().split("done")][:-1]
    

    Output:

    [['eggs'], ['bacon'], ['ham', 'cheese']]
    

    In case you wanted number-string indices, you could use this:

    with open("yourFile") as fd:
        l = [x.strip().split() for x in fd.read().split("done")][:-1]
        print dict(zip(map(str,range(len(l))),l))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a quick function that will console.log a variable name and
In Perl, is it possible to create a global variable based on a string?
Is this possible to create new {variable = x.something} and specify variable name dynamically?
Is it possible to create a template function that takes a variable number of
Is is possible to create/use a regular expression pattern in ruby that is based
I want to create a makefile variable that is a multi-line string (e.g. the
Is it possible to create a property in C# that has a variable number
Possible Duplicate: create multiple variables based on an int count Objective C Equivalent of
I'm trying to understand if it's possible to create a set of variables that
Is it possible a lambda function to have variable number of arguments? For example,

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.