0

Im trying to create a function that takes a list and assigns each of the strings in the list to a variable even when you dont know how many strings are in the list

Here is something i tried:

ExampleList = ['turtle','cow','goat','pig','swag']

def add_One(list):
    x = "a"+"1"
    y = 0
    y = y+1
    x = list[y]


while True:
    add_One(ExampleList)

so basically im taking the example list then im using a1 to define ExampleList[1] then i want it to loop and assign a11 to ExampleList[2] and so on

for an output im trying to get:

a1 = ExampleList[1]
a11 = ExampleList[2]
a111 = ExampleList[3]
a1111 = ExampleList[4]

and so on

i know this isnt the right way to do it but im trying to show you guys what i was trying to do

If anyone knows how to do this correctly please help!

Serial
  • 7,925
  • 13
  • 52
  • 71
  • 2
    For one thing, you shouldn't use `list` as a variable name -- it names a type in python :-) – Dan Lecocq May 09 '13 at 22:13
  • 1
    Could you add what you're expecting to get as output to the question? – Dan Lecocq May 09 '13 at 22:14
  • yeah youre right it was just an example – Serial May 09 '13 at 22:14
  • 3
    Why would you want to do this? The list elements are perfectly accessible and useable as is. ...you don't need to give them each their own variable name. – Gerrat May 09 '13 at 22:14
  • @Gerrat: Furthermore, you have no way of accessing those variables without using locals() – Eric May 09 '13 at 22:15
  • Basically im just asking how i can loop a function and have a differnt output each time i guess what i wrote is just an example!! – Serial May 09 '13 at 22:17
  • 3
    Are you trying to create variables dynamically? If so, as @Gerrat says, you almost never want to do that… but if your really do, it's a dup of dozens of other questions. Even though [Creating dynamically named variables form user input](http://stackoverflow.com/questions/11354214/creating-dynamically-named-variables-from-user-input) was closed, I'd suggest looking there first, because it has links to many of the other dups. – abarnert May 09 '13 at 22:17
  • Oh ok well if its not a reasonable question i guess nevermind then sorry i was just messing arond and came across this and thought id ask a question ok thanks! – Serial May 09 '13 at 22:20
  • 1
    @ChristianCareaga: If you can explain _why_ you think you want this, or show us a real program that you think needs it, we can explain specifically what you probably want to do instead (or, if you've found one of the very rare cases where it's a good idea, explain how to do it). More generally, see [What is the XY problem?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – abarnert May 09 '13 at 22:23
  • 1
    In case you're wondering, some cases where it _can_ be useful include: https://github.com/lihaoyi/macropy, an interactive interpreter that shares state between the interpreter and the interpreter code, generating wrappers around namespaces from other runtimes (e.g., a JVM or Smalltalk), etc. The fact that such cases exist is why Python allows you to dynamically add stuff to `globals` (and `builtins`), and `eval` and `exec`, and other things that are usually-but-not-always the wrong thing to do. – abarnert May 09 '13 at 22:29

3 Answers3

3

I think this is what you're trying to do. I don't know why on Earth you're trying to do it, but you can do it like this:

example_list = ['turtle','cow','goat','pig','swag']
number_of_ones = 1
for item in example_list:
    globals()['a'+('1'*number_of_ones)] = item
    number_of_ones += 1

print(a11111) # prints 'swag'

If you want it to be a little shorter, use enumerate:

example_list = ['turtle','cow','goat','pig','swag']
for number_of_ones, item in enumerate(example_list, 1):
    globals()['a'+('1'*i)] = item

print(a11111) # prints 'swag'
Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
2

Is this good enough?

vars = {}
for i, value in enumerate(example_list, 1):
    vars['a' + '1'*i] = value

print vars['a111']

If you really wanted to, you could then do

globals().update(vars)
Eric
  • 95,302
  • 53
  • 242
  • 374
1

for an output im trying to get:

a1 = ExampleList[1]
a11 = ExampleList[2]
a111 = ExampleList[3]
a1111 = ExampleList[4]

If you literally want that as output, either printed out or returned as a string, this is just a string formatting question, except for one twist: You need to keep track of some persistent state across calls. The best way to do something like that is with a generator, but it's possible to do it directly if you want. For example:

def add_One(lst, accumulated_values=[0, "a"]):
    accumulated_values[0] += 1
    accumulated_values[1] += '1'
    print('{} = ExampleList[{}]'.format(*accumulated_values))

If you mean that you're trying to create variables named a1, a11, etc., see Creating dynamically named variables from user input and the many duplicates all over this site for (a) why you really don't want to do that, (b) how to do it if you must, and (c) why you really don't want to do that even though you think you must.

Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • no i was just say after the program runs i can use those variables to represent the list strings – Serial May 09 '13 at 22:22
  • and now that everyone is saying its a dumb question its fine im a beginner i didnt, know i got it – Serial May 09 '13 at 22:23