Search code examples
pythonlistiterable-unpacking

Python: Assign variables from array


I have some code in Python:

repost_pid = row[0]
repost_permalink = row[1]
repost_domain = row[2]
repost_title = row[3]
repost_submitter = row[4]

Is there a one-liner way to assign these variables?

Also, what would I do if I wanted to skip a value?


Solution

  • Yes you can separate each variable with a , to perform unpacking

    repost_pid, repost_permalink, repost_domain, repost_title, repost_submitter = row
    

    If there is a particular value that you are not concerned about, the convention is to assign it to an underscore variable, e.g.

    repost_pid, repost_permalink, _, repost_title, repost_submitter = row