0

I want to convert strings into blank list objects, which can then be updated/appended. How do I do this / can it even be done?

Imagine I have a string, 'blah'. I want to change this string, so that 'blah' becomes the object name of a blank list. In other words:

'blah'

def some_function('blah'):
   ...
   ...
   return(blah)

some_function(blah)

OUTPUT: blah = []

I know that if I pass

list('blah')

this returns

['b','l','a','h']

But instead I want the opposite, where

list('blah')

returns

blah = []

2 Answers2

0

Moving on from the comments, a more logical way out could be to use a dict:

d = {}                         # empty dict

s = 'blah'                     # str
d[s] = []                      # empty list with the name of the str, blah = []

d[s].append("hello there!")    # appending to `blah`
print(d[s])
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

You can't in any meaningful way. I'd suggest heeding @Mark Tolonen's advice and using a dictionary like so:

dictionary = {}
dictionary['blah'] = []

If you'd now like to add to 'blah', you can use append():

dictionary['blah'].append(x)
Alec
  • 8,529
  • 8
  • 37
  • 63