0

I want to define lists at running-time using strings input by the user as their names. Example:

User input        >> "Cars"
python should do  >> cars[]
so later i can do >> cars.append("BMW")

Thanks.

user190081
  • 222
  • 2
  • 7
  • 1
    You shouldn't use dynamic variable names like this. – juanpa.arrivillaga Sep 21 '18 at 16:01
  • globals()[user_input] = [] wher user input is your user input, buuuuuuuuuuuuuuuuuuuuuut be careful – E.Serra Sep 21 '18 at 16:02
  • @juanpa.arrivillaga Why not? – user190081 Sep 21 '18 at 16:03
  • @user190081 because it is considered a bad practice. How will you refer to these variables in code? You'll end up hackily using the `globals()` dict, but why? Just use your own `dict` object. – juanpa.arrivillaga Sep 21 '18 at 16:05
  • In this situation, the proper solution is almost always to use a dict, or perhaps a list. For more info on this topic, please see [Why you don't want to dynamically create variables](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html). – PM 2Ring Sep 21 '18 at 16:09

0 Answers0