0

I want to create as many empty lists as there are integers in lst (a list of integers) and add those integers in lst to the end of each variable assigned to each empty list:

def create_3C_object_lists( lst ):
        """ creates all necessary lists """
        for i in range(len(lst)):
                exec("globals()['_3C_%d'] = []" % (lst[i]))
                print(type(_3C_%d)) % (lst[i])

What I hope to get:

_3C_(integer at index 0 in lst)
_3C_(integer at index 1 in lst)
...

and so on. Is there a better way of doing this? Perhaps without "exec" ?

4
  • 1
    You are better off making a list than making a bunch of variables with similar names. Commented Nov 8, 2014 at 8:19
  • I need a bunch of lists though, for later reference in my program. I'm appending things to these lists from a much larger list based on the numbers at the end so later I can create combinations via what is in these lists. There are a lot of items and it would be simpler for me to visualize if I could simply categorize them via these lists. Commented Nov 8, 2014 at 8:26
  • 1
    You can make a list of lists. Commented Nov 8, 2014 at 8:27
  • Ive thought of that, but geez...that'd be one looong list of lists :( Commented Nov 8, 2014 at 8:28

1 Answer 1

2

Just create a dictionary:

_3C_ = {i: [] for i in lst}
# Use it like this:
_3C_[17].append(...)

If for some reason you really need a variable, you can do it without exec like this:

globals()['_3C_{}'.format(your_integer)] = []
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.