0

after much googling i now ask. Is there a way to append an integer to the end of a variable name. Essentially creating a new variable with each iteration of a for loop. IE:

def parser(lst):
    count = 0
    for obj in lst:
        if isinstance(obj,list):
            parser(obj)
        else:
            for string in obj:
                var+count = o

This is what i get when i try to run above code:

SyntaxError: can't assign to operator
Matt
  • 37
  • 7
  • In order to help you fix the problem, please show us valid piece code to reproduce the problem and explain clearly what exactly you are trying to do. – thefourtheye Jan 06 '14 at 04:14
  • This is a duplicate of a large number of questions on SO, although that may not be obvious to someone who doesn't know the answer. The blog posts [Keep data out of your variable names](http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html) and [What you don't want to dynamically create variables](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html) explain why this is a bad design, and show you how to do it on the rare occasions when it's necessary, and link to a bunch of other SO questions with useful answers. – abarnert Jan 06 '14 at 06:34
  • @thefourtheye The data is just my gmail contacts in "outlook csv" form. The above code was just for experimental purposes to see if it was possible. But seeing as it is bad practce to do so, there is no point in doing it. – Matt Jan 06 '14 at 19:51
  • @abarnert I apologize for the duplicate question. I was unable to find one that answered my exact question. Thank you for the links to the blog posts. – Matt Jan 06 '14 at 20:01
  • @Matt: No need to apologize. As I said, the duplicates may not be obvious to someone who doesn't already know the answer. There are a few cases like this in Python (a lot fewer than in most other languages…) that come up over and over again because anyone who first runs into the problem can't figure out what to search for. – abarnert Jan 06 '14 at 20:15
  • @abarnert True, I did however find this page that may help me more. I will further look into it. http://docs.python.org/dev/library/csv – Matt Jan 06 '14 at 20:18

2 Answers2

6

You almost certainly want to use a list:

def parser(lst):
    vars = []
    for obj in data: # did you mean "for obj in lst:"?
        if isinstance(obj,list):
            parser(obj)
        else:
            for string in obj:
                vars.append(o) # did you mean "vars.append(string)"?

Then, instead of, say, var5, you would use vars[5].

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • +1 but missing SSCCE. cving... – thefourtheye Jan 06 '14 at 04:13
  • @DoorknobofSnow Thank you. Also You were correct in you assumption, 'data' was supposed to be 'lst'. 'Data' was the name of the ar holding the cleaned up "data". Thanks for catching that, Post edited. – Matt Jan 06 '14 at 20:06
1

Doorknob of Snow's answer is correct, but for completeness, you can create a new variable using locals()[var + str(count)] = o. But this is a bad idea, so don't.

aquavitae
  • 17,414
  • 11
  • 63
  • 106