0

Code snippet:
data = [("a", "b", "c", "d"),("e", "f", "g", "h")] for i in data: console = '{}_{}'.join(('consoleip',str(i[2]))) <> = Hostdata(hw_id = i[0],location_id = i[1],consoleip = i[2], biosversion = i[3])
Expected result:
consolip_c = Hostdata(hw_id ='a', location_id ='b', consoleip ='c', biosversion ='d') consolip_g = Hostdata(hw_id ='e', location_id ='f', consoleip ='g', biosversion ='h')

Im looping through the data, which holds thousands of database records and I want each record to be held in a distinct variable unique to the record. This would help me access each record with a unique named tuple variable.
How can I fill in the gap for <>, to assign a the namedtuple(Hostdata) to the value associated for console. Because I get a "SyntaxError: can't assign to function call" when I directly use:

'{}_{}'.join(('consoleip',str(i[2]))) = Hostdata(hw_id = i[0],location_id = i[1],consoleip = i[2],biosversion = i[3])
Murphy
  • 536
  • 6
  • 13
  • Is *"the value associated for console"* a really weird way of saying you want to print out the resulting value to the terminal? – tripleee Jan 30 '18 at 07:52
  • Please [edit] your question to indicate the expected result for a simple set of inputs. Code which doesn't do what you hope is a very poor way to communicate what you actually do hope to accomplish. – tripleee Jan 30 '18 at 08:07
  • "How do I dynamically create variables" is a very common beginner question, but the sane answer is almost always "use a dict" or "use a list". Search for duplicates. – tripleee Jan 30 '18 at 08:10
  • Okay thats correct https://stackoverflow.com/questions/4010840/generating-variable-names-on-fly-in-python provided a solution to use a dict that worked. Thanks – Murphy Jan 30 '18 at 08:32

1 Answers1

0

Try this:

console=Hostdata(hw_id = i[0],location_id = i[1],consoleip = i[2],biosversion = i[3])

You are trying to assign the value to the value that the variable holds instead of the value to the variable. Basically, the <> is console.

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44