Day 86: Converting nested list to list python
This took me a little less than an hour to resolve.
I got to share this.
I requested a json file from API.
The file was successfully downloaded.
Converted the .json to a regular dictionary in Python.
Tried to use the data which was in the form of a nested list.
It didn’t work.
How the file looked like?
Looks normal right?
I then tried to convert this into a dictionary
The lenght of this dictionary was 4.
This corresponded to the 4 keys that were in the dictionary.
I then checked the size of the list
The size was 5600+ in size.
This was weird
This list contained hundreds of data, not multiple thousands.
I then did a length check of the dictionary.
It returned that the type of the list was ‘str’
This was by far the biggest clue here.
There was a problem.
I tried to take a sample of that data.
Just a section and try to parse it and it was properly detected as list.
I tried converting the string to list
It was a list now but still contained 5600+ elements.
I tried separating elements of the list with a space and comma
So, it would be exactly like what was correctly recognized as list.
This didn’t work, I was shocked.
The solution was just to convert the list (which was in string format) to a list using the json library.
import json json.loads(mylist)
That solution was ridiculously simple and somehow took me an hour to find out…
And you are welcome !