0

I am trying to create a variable in python with prefix as list and then number which will be generated dynamically in the script. For example

I am trying to have a list10 where list is the prefix and 10 is the number generated dynamically.

In TCL we give like

list${i}

Is there same way in python too?

Sumit
  • 1,953
  • 6
  • 32
  • 58
  • 1
    Use a `list`! `mylist.append(myvalue)` – deceze Mar 30 '16 at 12:21
  • Yes, the way to do this in Python and Tcl is not to use sequential variable names. It is a terrible practice since it turns data into code. Yes, tcl allows you to do it, but `dict set mydict $i [list 4 5 6]` is far better as are the Python answers below. – msw Mar 30 '16 at 13:03

2 Answers2

4

The pythonic way to do this would be to make a dictionary to store your lists with the generated names as the dictionary's keys:

d = {}
d['list1'] = [1, 2, 3]
d['list2'] = ['a', 'b', 'c']

EDIT: generate key names

You can create keys like this:

key = 'list' + str(1)  # or whatever number you are using
d[key] = [your list]

END EDIT

Or if you don't really need to know the names, store your lists in a list and retrieve them by index:

lists = [[1, 2, 3], ['a', 'b', 'c']]

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • How to get list1, list2 etc as 1 and 2 are also made dynamically in the script. – Sumit Mar 30 '16 at 12:26
  • @Nitesh `'list' + str(1)`...!? Or, again, simply a *list*, which takes care of indices itself?! – deceze Mar 30 '16 at 12:28
  • I think it is not solving purpose. Can I append in dict keys. Say I have declared dict key as dict[key1]. I want to append in this. I am trying as dict[key1].append = value. But it is givng error – Sumit Mar 30 '16 at 12:42
  • Please open a new question explaining what you really want to do and showing your code - the question that you asked has been answered. – snakecharmerb Mar 30 '16 at 12:44
0

You can use locals(), vars(), or globals() and inject your variable name there. For eg.

>>> list10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'list10' is not defined
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'x': [], '__name__': '__main__', '__doc__': None}
>>> locals()['list10'] = []
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, 'list10': [], '__package__': None, 'x': [], '__name__': '__main__', '__doc__': None}
>>> list10
[]

Generally, if you're doing something like this, you'd probably be better off with using a dictionary to store the variable name and the value(s).

For eg.

>>> my_lists = {}
>>> my_lists['list10'] = []

And then when you want to look it up, you can .get() it if you want robustness against a variable name not existing, or directly accessing it if you're going to guard against non-existence yourself.

>>> the_list_i_want = my_lists.get('list10')
>>> the_list_i_want = my_lists['list10']  # Will raise a KeyError if it does not exist
Christian Witts
  • 11,375
  • 1
  • 33
  • 46
  • 4
    Using [`locals()`](https://docs.python.org/2/library/functions.html#locals) for this is specifically warned against in the documentation. – Eugene Yarmash Mar 30 '16 at 12:23