PARSE LIST OF LISTS OF DICTIONARIES USING PYTHON | JSON | NESTED LISTS (2023)

Introduction

This video will go over ๐ฅ๐ข๐ฌ๐ญ ๐จ๐Ÿ ๐ฅ๐ข๐ฌ๐ญ ๐จ๐Ÿ ๐๐ข๐œ๐ญ๐ข๐จ๐ง๐š๐ซ๐ข๐ž๐ฌ (nested lists of dictioanries), I will parse 3 different ways for the example with ๐๐ฒ๐ญ๐ก๐จ๐ง. There is also, a further nested dictionary with lists as values that will be parse and stored using default dictionary lists (this was tricky). Turn on the ๐Ÿ”” notification

Join this channel to get access to perks:
www.youtube.com/channel/UCbni-TDI-Ub8VlGaP8HLTNw/join

โžก Patreon: www.patreon.com/mrfugudatasci
โžก Buy Me A Coffee: www.buymeacoffee.com/mrfugudatasci
โžก Github: github.com/MrFuguDataScience
โžก Twitter: @MrFuguDataSci
โžก Instagram: @mrfugudatascience

Code From Today:
github.com/MrFuguDataScience/JSON/blob/master/Json%20_listsOfdict.ipynb

๐€๐ฆ๐š๐ณ๐จ๐ง ๐€๐Ÿ๐Ÿ๐ข๐ฅ๐ข๐š๐ญ๐ž ๐‹๐ข๐ง๐ค๐ฌ: (I receive a small commission on purchases)
* Prices & Availability Subject to change
--------------------------------------------
Audible Gift Membership: amzn.to/3pAfw7W (End Date: On Going)

Try Audible: amzn.to/3PETRWS (End Date: On Going)

Apple Certified Type C Charger & USB Wall Charger 20W with 2 cables: amzn.to/3dMdqPA

๐•๐ข๐๐ž๐จ๐ฌ ๐˜๐จ๐ฎ ๐Œ๐š๐ฒ ๐€๐ฅ๐ฌ๐จ ๐‹๐ข๐ค๐ž

โ–ถ๏ธ PARSE JSON FROM AN API: USING PYTHON: youtu.be/iaN1FxjBuGk

โ–ถ๏ธ TWITTER API WITH PYTHON | NLTK BASICS (NLP): youtu.be/kS_0RNdUjrQ

โ–ถ๏ธ NLP Using R-Studio: youtu.be/TXhEl7LTQKo

โ–ถ๏ธ R-STUDIO: LABEL & ONE HOT ENCODING WITH MULITPLE EXAMPLES: youtu.be/ZTeGJpOSrQo

โ–ถ๏ธ REGULAR EXPRESSIONS (Regex) for Parsing ADDRESSES using Python:
youtu.be/nRq8J_aYKT0

Gif at end: Moos-media on pixabay.com

Thumbnail: pixabay.com/vectors/squirrel-reading-books-surprise-304021/

end screen: www.instagram.com/footage3.0/ and on pixabay

Music &. Intro Pic: Special Thanks

Pixabay: instagram (subscribe gif): @imotivationitas

Music: Oshoฬva - Tidal Dance on
Soundcloud: soundcloud.com/osh-va ,
youtube: www.youtube.com/channel/UC_uiomVq61yUos1FUuOdsTw

Content

Welcome everybody, you're watching mr fugu data science.

Today, we're parsing a list of lists of dictionaries.

We will solve one problem.

Multiple ways.

I got this question from a recent viewer and decided to make a video to try to help them.

We need to do a few imports:.

Let's import json, pandas, from collections, import default, dictionary, and chain map.

The data set we're using today comes from a previous example.

I did parsing New York times.

Api.

This is some raw data where each value or row;, if we are putting this into a data frame, is a list of dictionaries.

To get a clear picture of what these data look like;.

We will take the first entry and notice that in this list of dictionaries, we have a few different options.

But.

We notice that we have inside of this another list of dictionaries from one of our values.

So.

We will need to parse inside of the media further due to nesting.

Where, we'll notice that there are three url's, formats, heights, and widths.

Within, the media metadata.

For each entry;.

We have 20 entries, okay.

If.

We took for instance, one specific entry and threw it into a data.

Frame.

It would look something like this.

But.

Really.

What we would like to have: is all of these.

In one single row.

So you, you would essentially like to have one list of all the urls, a list of all the formats, the heights, and the widths for each entry.

Because.

This was from one entry, not three, okay.

So.

Essentially, what I would like is to create this url:, where we have a list of the three urls.

Assume, that this is the second entry, and this is their three urls and putting it onto one data frame for two rows for this.

And.

I just cut and paste this.

It's the same exact data just to illustrate what I'm trying to achieve later.

If.

You did not want it this way and wanted to expand it.

You could always do the pandas explode.

If that was something you wanted.

Just remember, it gives you more rows.

Let's get into our first coding for the chain map from collections., And.

Think of it as taking multiple dictionaries and basically condensing it into one.

There's.

A few useful examples of showing this for the official documentation.

Let's just call this multiple dictionary to single.

Store this as a list and let's iterate in the range of values within the length of data frame.

We would like to parse.

And, take the column of media:, where I'm going to store this as a dictionary and call in the chain, map.

Taking.

All of the arguments for the data frame that I would like to parse and then you're going to see something kind of funky.

So, I'm using the iterator but.

Then I'm also going through all rows and columns to the end, all right.

And.

Then I'm going to call in my good old trusty list and append a little old friend, and let's see what this looks, like.

Now.

This is going to get us most of the way.

So.

This spreads everything out from the media, column.

Mind you, we we're going to concatenate at the end and put everything together.

Because.

This is one column that we are parsing.

And.

If you notice, we still have the metadata that we need to take care of later.

We can do this another way and if you notice there are also rows that are completely null that we'll take care.

Of.

Let's do a list.

Comprehension.

Let's, just call this merge; and I just found this simple little function online on stack, overflow., So, let's iterate inside of this/ And, then we would like to return the dictionary with everything.

And.

Let's do our list comprehension for the items inside of our list.

Now, I'm going to I don't know, I'll, just say..

I'll, just call this: function, (func) with iterator to do a list.

Because we're going to iterate inside of the data frame as usual for the media.

And.

Then, let's make a temp variable and iterate through each piece., Let's append everything.

Let's call that out put it into a data frame and there we are.

We get the same exact thing doing a different way, okay.

What, you could do is check how fast each one of these operations is versus the amount of memory using a profiler and see which is more beneficial out of these two.

If you're curious or more inclined.

Now, let's do a super crazy version.

I would say.

But, don't get scared, it won't bite.

So, let's walk through it, so I need to create a default dictionary, I'm going to create a empty list and I'm going to iterate, like we've been doing through this specific column.

And.

What do I want to do?? I'm going to check if the length of this is greater than zero.

And? What's going on here? Well? Basically, each list can be empty or filled which will equal one, because, if you're looking at lists that we're iterating through some will have something some want.

So if it has something, then we will then iterate inside of it.

Take, our iterator to go through each individual one.

Going inside of a list of lists.

We're.

Taking this because, it's now going to be a dictionary converted into tuples using items.

Which.

Would be useful for creating our default dictionary.

So? I could just leave a note here, so we could understand that we're converting a dictionary to tuples and iterating.

We would like to append the index value as well as j.

Which is going to be our tuples.

And you'll see why this is important in a second.

Because.

Basically we're trying to keep the frame of reference using the index with the tuple.

Because.

If it is not like this list, we need to figure out a way to take care of that.

So, we're going to I'll make a note of this in a second.

I'm, going to create a tuple and I'm going to zip together the data frame for the first element and I'm going to take the keys.

What.

I'm trying to do here is anything that didn't fall into this.

I need to create a tuple that has the same keys that are inside of our dictionary and then multiply it by the length or pardon me.

Yeah, and the values are going to be multiplied by the length of the keys, so it matches up.

And.

I want to mention something very specific here:.

If you had dictionaries that had mismatching keys.

This would not be what you would want to do right, here.

Since.

I know that for this specific example, it does work.

I'm using it.

If.

It did not.

You just have to do it a little bit, differently.

Okay,! So, what's going on with this? We have our first index value and we have the type the subtype, the caption, copyright, approved, metadata, right.

Then does the same thing for all of these.

Now.

Let's skip down to number seven, and this is what I wanted to match up, because when I create the default dictionary, here's my key as the type and then each value.

I want it to line up properly, and if I didn't do this else statement, we would have had an issue.

So.

Let's finish this up and let's create our default dictionary for the first entry in the tuple is the key.

The second entry is our value.

Throw.

This into a data frame, see what that looks.

Like.

But, I made a mistake.

There we go.

So.

This looks like we're on the right track and we did the same exact thing.

Once again.

Let's scroll, down.

So, we did the same thing., That's, perfect.

Let's, just call this new df, clean this up a little bit.

So it's not ticking up on my page.

So.

We did pretty good here.

But.

I want to note something once again.

If.

You have a list of dictionaries of varying lengths.

You need to do one more step, all right.

Don't forget, that.

We will consider doing something.

Like: basically find the unique keys and then you could perform.

If you wanted a set operator.

So, then you would have all of the keys and then you could do an if else statement to say if it's there put this, if not do that.

So here is basically what was going on with creating our tuple, so it cooperated with the formatting type.

The innermost list of lists for a media metadata is very interesting.

This threw me off and took me some time.

That's why this video is delayed.

So.

Basically, what I'm doing is I'm creating this function, where I have my default dictionary once again.

And, I'm iterating through my data frame column of choice, checking if it's a string.

Basically, if we don't have a list of dictionaries, then it's going to be a string value, saying nope or whatever I declared, or you have some empty value or whatever.

Then.

We have to iterate through our dictionary; turn it into tuples again and use our default dictionary and return.

That.

I have to iterate inside of the column.

Do the same type of comparison and then throw in our function has our default dictionary.

Then.

I used an else statement: because.

I wanted to keep the formatting.

S entry number 7, 13 and 16, or something like that.

Do not have a list.

And.

They are strings.

So.

We take care of that by creating this zipped tuples for our keys and our values, and I chuck that together.

Then.

I create a data frame and expand it.

And.

So for each entry in this: remember there were three urls, three formats, heights, and widths.

These.

Last three columns are always the same.

So.

You have this height, and this particular width for this thumbnail.

You have this format with this height and this width, etc.

And.

It was difficult to distinguish.

What's going on with these, you can see that each one of them is different, all right.

So.

I know the formatting is there.

I have this set up as a list of strings.

So? If you further decide to expand this out and flatten it:, if you needed to you, have that option.

Easily, all right.

So just scroll back up, and I want to mention this.

I decided to print these out.

So.

I can actually do a comparison if you see 10/3 10/3, 10/29 10/29 and you look at them and they're all different because.

They have this different separator for these images or whatever they are, right.

It looks like I did the formatting for everything: properly.

Then we concatenate and put everything together.

The original data, the new data frame with the media, and the expanded url information.

Now.

We end up with the same amount of rows.

So that checks out.

But.

Instead, you have 32 columns because it's expanded out now.

All right.

So, let's scroll back to the top and look here's everything that worked out from our nested stuff that I wanted to retain as a list.

I kept this just as a reference.

And.

Then these short ones right here came from the original media, all right.

And, then the rest of them are just the original data.

All right.

That wasn't too bad of a video.

Last thoughts.

I'd just like to say:.

If you wanted to speed this up, consider using list comprehensions, vs loops.

Understand that there are times when indexed values forgot, the e.

When index values actually do matter, and you would like to preserve the order.

So.

You need to adjust your code and remind yourself of that.

And.

Finally, people could write elegant code.

All the time.

But, it just comes with experience.

But, do realize that you may see elegant code, but it may not always be the most efficient.

May not always solve the problem.

Faster, okay.

So, don't get hung up on thinking.

Your code has to look the best.

Just, leave doc, strings and notes.

So people understand what's going on.

But.

As always.

Thank you for watching this video.

I hope I brought utility to someone.

Please, like share and subscribe, and if you subscribe turn on that notification, bell.

I'll see you in the next video.

Bye.

FAQs

How to convert a list of dictionaries to a list of lists in Python? โ€บ

Step-by-step approach:
  1. Initialize an empty list to hold the final result.
  2. Extract the keys from the first dictionary in the input list and append them as a list to the final result list.
  3. Extract the values from each dictionary in the input list and append them as a list to the final result list using the zip() function.
Apr 6, 2023

How to extract data from a list of dictionaries in Python? โ€บ

Here are 3 approaches to extract dictionary values as a list in Python:
  1. (1) Using a list() function: my_list = list(my_dict.values())
  2. (2) Using a List Comprehension: my_list = [i for i in my_dict.values()]
  3. (3) Using For Loop: my_list = [] for i in my_dict.values(): my_list.append(i)

How to convert a list of dictionaries to a list of strings in Python? โ€บ

You can easily convert a Python dictionary to a string using the str() function. The str() function takes an object (since everything in Python is an object) as an input parameter and returns a string variant of that object.

How do I convert a list of lists to a single list? โ€บ

How to Convert a List of Lists Into A Single List
  1. List comprehension [x for l in lst for x in l] assuming you have a list of lists lst .
  2. Unpacking [*lst[0], *lst[1]] assuming you have a list of two lists lst .
  3. Using the extend() method of Python lists to extend all lists in the list of lists.
Apr 25, 2020

How do you create a list from a list of lists in Python? โ€บ

Python provides an option of creating a list within a list. If put simply, it is a nested list but with one or more lists inside as an element. Here, [a,b], [c,d], and [e,f] are separate lists which are passed as elements to make a new list.

How do I create a list from multiple dictionaries in Python? โ€บ

We can append a new dictionary to the list of dictionaries by using the Python append() method. Example: In this example, we have a list of a single dictionary element. We will add another diction to this list using append() method.

How to convert list using dictionary in Python? โ€บ

Converting a list to a dictionary can be done by using the list of key method. Create two lists, one for keys and one for values. Zip the two lists together using the zip() function to create a list of key-value pairs. Convert the list of key-value pairs to a dictionary using the dict() function.

How do you process a list of dictionaries in Python? โ€บ

Python list of Dictionaries
  1. #initialising a dictionary inside a list.
  2. list_val=[{'English':31101,'Hindi':31102,'Mathematics':31103,'Physics':31104,'Chemistry':31105}]
  3. #displaying the list.
  4. print("The dictionary inside list is: ",list_val)
  5. #checking the type.
  6. print("The type of list_val is: ",type(list_val))

How do I convert a list of dictionaries to CSV in Python? โ€บ

Step 1: Import the Pandas Module. Step 2: Convert the list of dictionaries to a Pandas dataframe. A Pandas dataframe is like a 2D array or a table with rows and columns. Step 3: Now, convert the pandas dataframe to a CSV file using the to_csv function.

How to create a list of dictionaries in Python using for loop? โ€บ

Python Dictionary with For Loop
  1. To iterate all over dict items you'll need fruits.items()
  2. To build a list repeating each key N times do: [key]*N.
  3. As dict values indicate how many times to repeat, do: [key]*value.
Oct 7, 2020

How to convert a list of dictionaries to DataFrame in Python? โ€บ

Use from_dict(), from_records(), json_normalize() methods to convert list of dictionaries (dict) to pandas DataFrame. Dict is a type in python to hold key-value pairs. Key is used as a column name and value is used for column value when we convert dict to DataFrame.

How to convert list of strings to list of lists in Python? โ€บ

Create an empty list x to store the result. Iterate over each element item and its index i in test_list using enumerate(). Use the eval() function to evaluate the string item as a Python expression and convert it into a list.

How do you combine two lists into a list of lists in Python? โ€บ

Method 1: Using nested for loop

Create a variable to store the input list of lists(nested list). Use the append() function(adds the element to the list at the end) to add this element to the result list. Printing the resultant list after joining the input list of lists.

How do I convert multiple lists to single lists? โ€บ

chain() function combines the multiple sublists into a single iterable object, and then the list() function converts the iterable object into a single list.

How do I make multiple lists into one list in Python? โ€บ

Python's extend() method can be used to concatenate two lists in Python. The extend() function does iterate over the passed parameter and adds the item to the list thus, extending the list in a linear fashion. All the elements of the list2 get appended to list1 and thus the list1 gets updated and results as output.

How do you convert a dictionary to a list in Python? โ€บ

Converting Dictionary to List?
  1. # Convert to a list of tuples (keys and values) items_list = list(my_dict.items()) print(items_list) # Output: [('a', 1), ('b', 2), ('c', 3)]
  2. items_list = [[k,v] for k,v in my_dict.items()] print(items_list) # Output: [['a', 1], ['b', 2], ['c', 3]]

How to convert a list of list of dictionary to a DataFrame in Python? โ€บ

Use pd. DataFrame. from_dict() to transform a list of dictionaries to pandas DatFrame. This function is used to construct DataFrame from dict of array-like or dicts.

How do you turn a list of dictionaries into a list of tuples? โ€บ

Approach:
  1. Import the chain() function from the itertools module.
  2. Use the chain() function to create a single iterable from the dictionaries in the ini_list.
  3. Use a list comprehension to create a list of tuples by combining each key and value pair with the + operator.
  4. Flatten the list of tuples.
Jun 24, 2023

How to convert a list with dictionaries in object Python? โ€บ

Using a for loop method

One way to convert a list to a dictionary python is to use a for loop to iterate through the list and create dictionary from list python and from its elements. In the above code, we create an empty dictionary my_dict , then loop through the list my_list Applying the enumerate() function.

Top Articles
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated: 20/11/2023

Views: 5239

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.