-2

I would like to be able to create individual items from a list.

For example

test_list = ["dog", "cat", 12, 34, "boop"]

#code code code

item_1 = "dog"
item_2 = "cat"
item_3 = 12
item_4 = 34
item_5 = "boop"

So I would like to run some loop that goes through the list and creates a new object for each item in the list. Is this possible?

Lafexlos
  • 7,618
  • 5
  • 38
  • 53
Natty
  • 21
  • 2
  • 3
    Is this possible? Yes! but what type of object and how you want? – Learner Jan 29 '16 at 14:46
  • 3
    It is possible to inject variables into global scope but I really wonder why you would do such a thing. This indicates something in your design is wrong down the path. (suppose you have your 5 variables. Now what are you going to do with them you can't do with a list?) –  Jan 29 '16 at 14:47
  • 2
    http://xyproblem.info/ – Karoly Horvath Jan 29 '16 at 14:51
  • Poor OP, I feel for you. Your question is not bad per se, it's just ringing the "design smell" bell into any seasoned developer's head. Don't take these downvotes too seriously. You clearly have an XY problem, and SO is probably not the right place to discuss where the actual problem comes from and how you can solve it - so your question is going to be closed in the end. But I asked myself exactly that type of question when I was new to programming. –  Jan 29 '16 at 14:58
  • 2
    See how "correct" answers get downvoted as well. ``globals()`` is the way to go *if you actually needed to do this*. We just *know* with 99.9% confidence you don't. Tell us what you are trying to achieve, and you might learn something valuable today. –  Jan 29 '16 at 15:02
  • 1
    It's also somewhat disappointing that none of the downvoters seems to explain just *why* it's commonly considered poor practice. – Frerich Raabe Jan 29 '16 at 15:02
  • Possibly duplicate of [Creating new variables in loop, with names from list, in Python](http://stackoverflow.com/questions/11319909/creating-new-variables-in-loop-with-names-from-list-in-python). Please check out this blog: [Why you don't want to dynamically create variables](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html) – Quinn Jan 29 '16 at 16:07
  • I am actually totally OK if this thread gets closed. It turns out that a dictionary really is the best way to accomplish what I needed to do. Thanks Tibo – Natty Jan 29 '16 at 18:20
  • Also, now I know what an XY question is... Sorry guys/girls! – Natty Jan 29 '16 at 18:21

2 Answers2

1

Don't do this. Either keep the list as is, because you can already get item number x with test_list[x-1] or build a dictionary:

>>> items = {'item_{}'.format(i):thing for i,thing in enumerate(test_list,1)}
>>> items['item_2']
'cat'

Since your names are not very meaningful (you are just offsetting the list index by one and adding 'item_' in front of it), I don't think building a dictionary is particularly necessary here.

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • From the research I did since I asked this, I have come to realize that you're right. A dictionary is the way to go. Thank you! – Natty Jan 29 '16 at 18:13
-1

You can use the dictionary returned by the globals() function to set variables like that:

test_list = ["dog", "cat", 12, 34, "boop"]

for i, x in enumerate(test_list):
    globals()["item_" + str(i + 1)] = x

print item_1
print item_2
print item_3
print item_4
print item_5
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • Instant downvote. Don't help the OP do stupid things. At the very least leave a fat bold warning that this is a horrible idea. – Karoly Horvath Jan 29 '16 at 14:55
  • 2
    If you think it's a stupid idea, you should down vote the question. This answer seems to address exactly the question which was asked. – Frerich Raabe Jan 29 '16 at 14:56
  • If you have problems with that, feel free to post on meta. – Karoly Horvath Jan 29 '16 at 14:57
  • 1
    @KarolyHorvath This issue has been [discussed extensively](http://meta.stackexchange.com/questions/98197/should-users-be-penalized-for-answering-bad-questions), the consensus appears to be that mass-downvoting answers on poor questions is not a good practice and 'Down-voting should be reserved for answers that are not useful.'. – Frerich Raabe Jan 29 '16 at 14:59
  • That's an answer for an off-topic question. I bet you'll get different responses if you're trying to help someone to shoot himself in the foot. – Karoly Horvath Jan 29 '16 at 15:02
  • @Frerich Raabe - Thanks! This is actually exactly what I was asking for. I'm sorry you got shamed for it. I actually asked this question for a friend and while I was waiting for a response we figured out that actually a dictionary was the right way to go. But you did actually answer my question. Thank you! – Natty Jan 29 '16 at 18:12