0

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]

2 Answers 2

5

You can use unpacking

folder, name, doc_id, page_num = row
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, I knew it was something simple..I was looking for 'list expansion' or something...
@roemhildtg If you liked this answer, consider checking it
0

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)

1 Comment

What makes you call this "more professional" than the other answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.