0

I want to create a simple config CSV file for my python script that will be loaded into a dictionary. Every line should represent a dictionary key with a list of values.

The structure will look like this:

key, value1, value2, value3, value4
key, value1, value2, value3, value4
key, value1, value2, value3, value4

A print (list) looks like this:

['key,value1,value2,value3,value4','key,value1,value2,value3,value4','key,value1,value2,value3,value4']

My function for reading input file gives me back a list with element per line. Here I try to build the dictionary with the split string function in a loop. But I cannot get the syntax right. I have found some similar examples, but they don't seem to work in python 3.

result = dict(line.split(",")[0],[line.split(",")[1:] for line in list)

1
  • Can you please provide some sample input? Commented May 16, 2020 at 12:56

1 Answer 1

2

Use a dict comprehension.
Oh, and try to avoid calling variables the same name as built-ins (so I've called it lst instead):

result = {line.split(',')[0]: line.split(',')[1:] for line in lst}
Sign up to request clarification or add additional context in comments.

Comments

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.