ZeroSoul13
ZeroSoul13

Reputation: 99

how to assign variable values directly from list range

I'm trying to find a better and more pythonic way to this piece of code:

for i in rows:
    row_data = i.findAll('td')
    serial = row_data[0]
    hostname = row_data[1]
    owner = row_data[2]
    memory = row_data[3]
    processor = row_data[4]
    os = row_data[5]
    model = row_data[6]
    ip = row_data[7]

i'm trying to do something like this:

 [serial, hostname, owner, memory, etc..] = row_data[:7]

Any ideas on how this can be achieved?

With or without index i get this message: [serial, hostname, owner, memory, processor, os, model, be_ip] = row_data ValueError: too many values to unpack

Upvotes: 1

Views: 58

Answers (1)

NPE
NPE

Reputation: 500713

You can do exactly that:

>>> row_data = ['serial', 'hostname', 'ip']
>>> [serial, hostname, ip] = row_data
>>> serial
'serial'
>>> hostname
'hostname'
>>> ip
'ip'

The square brackets around [serial, hostname, ip] are optional.

Upvotes: 6

Related Questions