0

Ex. Lets consider, I have a list,

list_var = ['sales_qty' , 'returns_qty' , 'net_sales_qty' , 'sales_amt' , 'returns_amt' ,'product_discount_amt' , 'product_net_amt' ,'product_cost_amt' , 'gross_sales_amt' , 'supplier_disc_amt' , 'category_disc_amt' , 'topup_disc_amt' , 'value_at_cost']

from this list element, i want to init these elements as list in the memory, so i could use append, extend method for these elements.

As follow,

#Need these variable in memory..
sales_qty = [], returns_qty  = [], net_sales_qty = [] ... value_at_cost = []

#list operation on variable..
sales_qty.append(5)

Is there any simple way to do that, so i could remove element or add element in list easily?

2
  • What exactly is sales_qty in list_var = [sales_qty, ...]? A reference to some object? Commented Jan 29, 2013 at 12:16
  • 1
    @NPE: this is string element in the list, for which i need to define as list. Commented Jan 29, 2013 at 12:18

3 Answers 3

5

You could create a dictionary of lists based on list_var .

list_var = ['sales_qty' , 'returns_qty' , 'net_sales_qty' , 'sales_amt' , 'returns_amt' ,'product_discount_amt' , 'product_net_amt' ,'product_cost_amt' , 'gross_sales_amt' , 'supplier_disc_amt' , 'category_disc_amt' , 'topup_disc_amt' , 'value_at_cost']

list_dic = {k: [] for k in list_var}

Now can access your lists through the list_dic dict:

list_dic['sales_qty'].append(123)
list_dic['category_disc_amt'].append('abc')

If you're using a python version that does not support dict comprehensions (python 2.6 or lower), you can use a list comprehension instead (as DSM explained in his comment):

list_dic = dict((k, []) for k in list_var)
Sign up to request clarification or add additional context in comments.

2 Comments

{k: [] for k in list_var} this dict format not supported for python2.5 :(
@Niks: then you can use dict((k, []) for k in list_var) instead. This is still the right approach.
2

Use vars if you are invoking from global scope. Otherwise, use globals.

list_var = ['sales_qty' , 'returns_qty' , 'net_sales_qty' , 'sales_amt' , 'returns_amt' ,'product_discount_amt' , 'product_net_amt' ,'product_cost_amt' , 'gross_sales_amt' , 'supplier_disc_amt' , 'category_disc_amt' , 'topup_disc_amt' , 'value_at_cost']

for item in list_var:
    vars()[item] = []

sales_qty.append(1)
sales_qty.append(3)
returns_qty.append(2)

print "Outside..."
print sales_qty
print returns_qty


def foo():
    global sales_qty

    sales_qty.append(4)

    print "Inside foo..."
    print sales_qty
    print returns_qty

    # This works if you want the whole thing inside a function
    print "Variable from inside foo..."
    globals()["new_var"] = []
    new_var.append(5)
    print new_var

foo()

print "Outside again..."
print sales_qty
print returns_qty

Output:

>>> 
Outside...
[1, 3]
[2]
Inside foo...
[1, 3, 4]
[2]
Variable from inside foo...
[5]
Outside again...
[1, 3, 4]
[2]
>>> 

8 Comments

Are these init as local var or global var in the memory??
They will be local to the scope in which vars() is invoked.
Now try this from within a function. As your link says: "Note, the locals dictionary is only useful for reads since updates to the locals dictionary are ignored." It only works by fluke in your test because you're working at module level.
Even if this would work, I don't see the point in creating variables dynamically. If you want to access them in your script, you need to know the name in advance, in which case you could just create them like any other ordinary variable: sales_qty = []
Yes, globals() will work -- and for that matter, using "exec" would work in local scope if you absolutely, positively, wanted to create local variables dynamically. [Why, I've no idea.] But now you not only have the problem that @DominicKexel pointed out, you're polluting the global scope. That's deeply unpythonic.
|
0

You mean something like this:

list_var = [ 'abc', 'cde', 'fgh' ]

dict_var = dict()
for x in list_var:
    dict_var[x] = []

dict_var['abc'].append(5)

1 Comment

Take note that your vars variable hides the builtin function vars. Nevertheless it can still be called as __builtins__.vars.

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.