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])
4
  • Is "the value associated for console" a really weird way of saying you want to print out the resulting value to the terminal? Commented Jan 30, 2018 at 7: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. Commented Jan 30, 2018 at 8: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. Commented Jan 30, 2018 at 8:10
  • Okay thats correct stackoverflow.com/questions/4010840/… provided a solution to use a dict that worked. Thanks Commented Jan 30, 2018 at 8:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

console = Hostdata(*i) is enough?
I think so. He seems to want to assign the above value to a variable. Right now, he is assigning it to a value which you cannot do
Are you suggesting variable reassignment? I really want to achieve "assign the value to the value that the variable holds"
That's like saying "I want "foo" = "bar" to succeed"
Im not sure if understand the requirement here. If you look at the code Im looping through the result, which holds thousands of database records and I want each record to be held in a distinct variable unique to the record.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.