1

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

ZeroSoul13
  • 99
  • 2
  • 9

1 Answers1

6

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012