Skip to content
Advertisement

How to append items in dictionary nested list?

I have a file with two columns of data, for example:

JavaScript

I want to obtain a dictionary with first column as the key and the second column as values, something like the following:

JavaScript

Advertisement

Answer

You haven’t specified full details of the content of your file. I am assuming you don’t have any column names. With that if you use pandas, the solution will look like:

JavaScript

read_fwf is used to read fixed width format files. the option header=None makes sure that the first row is not taken as column names.

Then you groupby the first column and aggregate the second column as list. Then use to_dict() to get a dictionary.

print(out):

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement