Why don't you just construct a dictionary using the strings as keys?

>>> class test():
    def handy(self):
        a = raw_input('How many hands? ')
        d = { "hand" + str(i + 1) : self.do_something(i) for i in range(int(a)) }

        keys = d.keys()
        keys.sort()
        for x in keys:
            print x, '=', d[x]

    def do_something(self, i):
        return "something " + str(i)

>>> test().handy()
How many hands? 4
hand1 = something 0
hand2 = something 1
hand3 = something 2
hand4 = something 3

Edit: You updated the question to ask if you can store a dictionary as a value in a dictionary. Yes, you can:

>>> d = { i : { j : str(i) + str(j) for j in range(5) } for i in range(5) }
>>> d[1][2]
'12'
>>> d[4][1]
'41'
>>> d[2]
{0: '20', 1: '21', 2: '22', 3: '23', 4: '24'}
>> d[5] = { 1 : '51' }
>> d[5][1]
'51'
Answer from verdesmarald on Stack Overflow
Discussions

In python create a list with a variable in the name - Stack Overflow
I'm trying to generate a series of (empty) lists using a for loop in python, and I want the name of the list to include a variable. e.g. y0, y1, y2 etc. Ideally looking something like this: ... You don't want to do that. See the answers for what to actually do. ... Creating empty data structures ... More on stackoverflow.com
🌐 stackoverflow.com
How to create a List given a variable "x" in Python? - Stack Overflow
Python: I have a variable say x. I need to create a list of name "x" More on stackoverflow.com
🌐 stackoverflow.com
List variable name involves different variable outputs
Hi, i have a couple of lists to be appended to a particular list dynamically: The desied result should be categorized=[[‘Neutral’,‘elderly’],[‘Short’,‘woman’],[‘tall’,‘man’]] i just wonder if variable outputs can be a part of anthor variable name? More on discuss.python.org
🌐 discuss.python.org
4
0
September 11, 2024
python - How can I create new variables in a loop, with the names and values coming from a list? - Stack Overflow
How can I create new variables with the names and values coming from a list? More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
Top answer
1 of 2
24

This is a bad idea. You should not dynamically create variable names, use a dictionary instead:

Copyvariables = {}
for name, colour, shape in Applist:
    variables[name + "_n"] = name
    variables[name + "_c"] = colour
    variables[name + "_s"] = shape

Now access them as variables["Apple_n"], etc.

What you really want though, is perhaps a dict of dicts:

Copyvariables = {}
for name, colour, shape in Applist:
    variables[name] = {"name": name, "colour": colour, "shape": shape}

print "Apple shape: " + variables["Apple"]["shape"]

Or, perhaps even better, a namedtuple:

Copyfrom collections import namedtuple

variables = {}
Fruit = namedtuple("Fruit", ["name", "colour", "shape"])
for args in Applist:
    fruit = Fruit(*args)
    variables[fruit.name] = fruit

print "Apple shape: " + variables["Apple"].shape

You can't change the variables of each Fruit if you use a namedtuple though (i.e. no setting variables["Apple"].colour to "green"), so it is perhaps not a good solution, depending on the intended usage. If you like the namedtuple solution but want to change the variables, you can make it a full-blown Fruit class instead, which can be used as a drop-in replacement for the namedtuple Fruit in the above code.

Copyclass Fruit(object):
    def __init__(self, name, colour, shape):
        self.name = name
        self.colour = colour
        self.shape = shape
2 of 2
2

It would be easiest to do this with a dictionary:

Copyapp_list = [
    ['Apple', 'red', 'circle'],
    ['Banana', 'yellow', 'abnormal'],
    ['Pear', 'green', 'abnormal']
]
app_keys = {}

for sub_list in app_list:
    app_keys["%s_n" % sub_list[0]] = sub_list[0]
    app_keys["%s_c" % sub_list[0]] = sub_list[1]
    app_keys["%s_s" % sub_list[0]] = sub_list[2]
🌐
Reuven Lerner
lerner.co.il › home › blog › python › playing with python strings, lists, and variable names — or, a complex answer to a simple question
Playing with Python strings, lists, and variable names — or, a complex answer to a simple question
June 13, 2019 - But if you really and truly want to create variables based on the values in the string, then we’ll have to use a few more tricks. One is to take advantage of the fact that global variables are actually stored in a dictionary. Yes, that’s right — you might think that when you write “x=100” that you’re storing things in some magical location. But actually, Python turns your variable name into a string, and uses that string as a key into a dictionary.
🌐
Real Python
realpython.com › python-variables
Variables in Python: Usage and Best Practices – Real Python
January 12, 2025 - The primary way to create a variable in Python is to assign it a value using the assignment operator and the following syntax: ... In this syntax, you have the variable’s name on the left, then the assignment (=) operator, followed by the ...
Find elsewhere
🌐
Google
developers.google.com › google for education › python › python lists
Python Lists | Python Education | Google for Developers
January 23, 2026 - Python's *for* and *in* constructs are extremely useful, and the first use of them we'll see is with lists. The *for* construct -- for var in list -- is an easy way to look at each element in a list (or other collection). Do not add or remove from the list during iteration. squares = [1, 4, 9, 16] sum = 0 for num in squares: sum += num print(sum) ## 30 · If you know what sort of thing is in the list, use a variable name in the loop that captures that information such as "num", or "name", or "url".
🌐
Cisco
ipcisco.com › home › python variables
Python Variables | Python Variable Names | How to Assign Value?⋆ IpCisco
August 5, 2022 - So, the variable that you create with lower case is not the same variable that is created with upper case. As an example, a and A are two different variables in python programming. ... Now, to see how to use variables in Python, let’s do a simple example in which we will assign different values to different variables. a = 5 b = "John" abc = {1,2,3,4,5} mylist = ["x","yy","zzz"] print(a) print(b) print(abc) print(mylist) ... You can also watch the video of this lesson! We can change the value of a variable multiple times in our code.
🌐
DataCamp
datacamp.com › tutorial › lists-python
Python Lists Tutorial | DataCamp
June 25, 2020 - # Area variables (in square meters) hall = 11.25 kit = 18.0 liv = 20.0 bed = 10.75 bath = 9.50 # Create list areas areas = [hall, kit, liv, bed, bath] # Print areas print(areas) When you run the above code, it produces the following result: ... Try it for yourself. To learn more about creating lists, please see this video from our course Introduction to Python.
🌐
Reddit
reddit.com › r/learnpython › creating different list names automatically
r/learnpython on Reddit: creating different list names automatically
September 29, 2023 -

Hello , I'm trying to create several name lists automatically . because I want to to create a data frame with them. For example :

List_1= [1,2,3]

List_2= [10,11,12]

etc....

afterwards I would like to create a unique dictionary for every one of them so that I can convert it into a dataframe with pandas

dic1= {'A': List_1, 'B': List_2. etc...}

but Im having trouble creating the unique list names, I have tried using formatted strings with for loop, but Im just getting the name and not the data points. I would really appreciate your help in this matter. Thank u

🌐
GeeksforGeeks
geeksforgeeks.org › python-construct-variables-of-list-elements
Python - Construct variables of list elements - GeeksforGeeks
April 5, 2023 - For each index i, use the update() method with globals() to create a new variable with the name of the corresponding element in test_list1 and the value of the corresponding element in test_list2.