twoLeftFeet
twoLeftFeet

Reputation: 720

How can I convert a list to named variables in Python?

Is there a quick way in python to convert a list of values to named variables?

For example:

row = ['files', 'file.pdf', 'D1234', 43]      

folder = row[0]
name = row[1]
doc_id = row[2]
page_num = row[3]

Upvotes: 0

Views: 119

Answers (2)

rkoots
rkoots

Reputation: 213

You can use this below patterns to do it more professionally

v = ['files', 'file.pdf', 'D1234', 43]    
keys = ['folder', 'name', 'doc_id', 'page_num']
junk = map(lambda k, v: dict.update({k: v}), keys, values)

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117866

You can use unpacking

folder, name, doc_id, page_num = row

Upvotes: 5

Related Questions