Python Lists, Tuples, Dictionaries | Real Estate Analytics

In this video, we will cover Python lists, dictionaries, and tuples. You will learn different ways on how to store data.

Video Block
Double-click here to add a video by URL or embed code. Learn more

Unknown Speaker 0:00

In this video, we will cover Python lists, dictionaries, and tuples. You will learn different ways on how to store data. Let's jump into Lesson Two from our notebook. In our table of contents, what are lists, lists are used to store multiple items and a single value. We had a preview of lists in our last section, when we were splitting our street address into street, city, and state lists are one of four built in data types and Python used to store collections of data. The three other types are tuples, sets and dictionaries, we will cover two of the three down below, lists are created using square brackets. And this first cell, you could see that we built a list of strategies, specifically investment strategies. This includes fix and flip, Burr, buy and hold and house hacking. Let's count the elements of in our list. We can use pythons built in function Len to get the length of our list.

Unknown Speaker 1:13

When we run this cell, we could see that we have four different objects in our list, which is true. Now what if we want to get the first three investment strategies? How do we actually index our list while indexing our list is very similar to how we index strings, let's type out our variable name.

Unknown Speaker 1:35

Then use square brackets. And if we want to get the first three investment strategies, we would select colon and then three, now run our cell.

Unknown Speaker 1:48

And we can see the output are the first three elements of in our string.

Unknown Speaker 1:54

Now what if we want to print every single investment strategy, we can use a for loop in order to do this, the way a for loop works is you first enter the word for

Unknown Speaker 2:06

then you have your list that you want to iterate through. So in this case, strategies underscore list.

Unknown Speaker 2:16

Then you add a colon afterwards.

Unknown Speaker 2:20

Now we have here for and then our list. But what do we want to do, we want to do something with every single object of interest. So we're going to call our objects X and state x n.

Unknown Speaker 2:34

So this is telling Python do something for every element in our list. And this case, we're just going to print the element. So let's add a print statement below.

Unknown Speaker 2:46

By running our code, we can see that Python now printed every single element in our list. And we'll touch on for loops more in the following lessons. But I just want you to see the power of using lists and having elements stored because you could then reference them later on. Next, let's switch it up to a list of all integers. In this case, we have numeric values, and they represent the rental income across five different properties. So let's put what the length of our rental list is, as we know, we would use the len function and then wrap it with our list inside.

Unknown Speaker 3:27

The number of units that we have of rental income in our list is five units in total. Now what if we want to sort these values from low to high, we could use the built in function dot sort on our list.

Unknown Speaker 3:47

Here we can see the values are now sorted from the lowest amount of rent income to the highest. So we have 750 to 1750. Now what if we want to just say what is the lowest rental income in our list and what is the highest now that our values are already sorted, we can index and state the first element and last element.

Unknown Speaker 4:18

If you remember from our last lesson, when we use zero, we select the first element. And negative one will allow us to select the last element which is similar to how we did it with strings. And with lists it's the same way. So we see our lowest rental income is 750 and highest to 1750. Let's move on to a scenario. We are partnering with another investor, we're going to merge our portfolios together to increase the number of properties we have. So our second investor has a new list called rent and comm list two, and they have three rental properties here. So how do we actually merge lists together? Well, we can do this by

Unknown Speaker 5:00

Adding a plus sign in between the two. So we're going to create a new variable called full income lists that combines these two lists together.

Unknown Speaker 5:18

We can then view the contents of this new full list by copying our new variable and pasting it in our print statement. We can also look at the count that we're going to have of total rentals now that we've merged these two together by wrapping it in the len function, now run the cell. And we could see that our full income list now has eight different elements. Or you could say, eight different rental properties combined. Now if we want to understand this new portfolio a little bit more, we may be curious on what's the total amount of rental income that's coming in, we can get this value by using the sum function on our list.

Unknown Speaker 6:02

We can see here in total, our full income for our rental properties is 11,002 50. This is before any expenses. What is their average rental income? Can we also figure this out? Yes, well, we have already the sum, in order to get the average, we had to divide the sum by the total amount of elements within our list, which we have up top. So we're just going to divide these two to get the average, I will copy the sum here.

Unknown Speaker 6:35

And then I will copy the length of our list and use a divide symbol, which is a forward slash run ourselves. And we could see that the average rental income between all eight units is about $1,400. This is a useful way for us to see how we can summarize our lists. As a heads up we will use lists a lot within this course. And in the real world when you're using Python. The next section we're looking at here are tuples tuples, are used to store multiple items in a single variable, a tuples, a collection, which is ordered and unchanged. So think of it as a list except the order of it is static. In this case, let's imagine that we have a list of different properties. And the streets are here, the topple is ordered based on the year they were acquired for each of these properties. That's why we would want it in a tupple in this situation so that these valleys do not change around. Now we can print the number of properties by using Len.

Unknown Speaker 7:44

We could see we have five properties in total. If we want to print out our values, we can take and copy our variable, and then run it within our statement. And we could see all the variables return, we won't be touching tuples later on in the course mostly list. But the one that we definitely will be using a lot are dictionaries, which is one of my favorite objects. dictionaries are used to store data values and key value pairs. A dictionary is a collection which is ordered, changeable and does not allow for duplicates. Dictionary items are presented in key value pairs and can be referred to by using the key name in this first cell, we have a dictionary, we can run it, and I will explain what are in the contents. So our dictionary is called property underscore dict. And then we have an equal sign. And in order to create a dictionary, we wrap it in these curly braces. Now we have our string, which is same up top 123 Main Street. But now we have a lot more information about our string. The colon represents the difference between the key which is our street and the values in it. So we have a string as our key. And then we have a nested dictionary. So it's another dictionary. And we have key here, which is bought on some say When did I buy is 123 Main Street. This case, I bought it in 2019.

Unknown Speaker 9:18

And our next key is rent income. What is the rent income for this property? And remember, we looked at this earlier on when we were looking at lists, and this case it was 750. So this is a better way of just storing our data so that we can reflect back and be able to actually grab these values based on their key value pairs. How do we actually do this? Well, first, let's look at what the type of our variable is. So in this cell, we could wrap type and property dict.

Unknown Speaker 9:51

Run enter. And we could see that the type of our object is a dictionary which makes sense. Now the way dictionaries

Unknown Speaker 10:00

to work because they have keys and values, we can view the keys and the values by adding at the end dot keys. So for our keys, we have five different keys. And these are all the street addresses. Now if we want to view the values, it's the same code. And now we could see all the values that represent that come from the key itself. Now let's try a little bit of an advanced step. How do we actually iterate through our dictionary. So beforehand, when we were looking at lists of top, we wanted to print every investment strategy. So we iterated through every element in our list, and we printed it out. Now what if we want to do the same thing when it comes to our dictionary, in order to do this, we would also start with a for loop. So for every key and property dict dot keys, we will print a statement, because we will touch on four loops. In future lessons, I'm just going to copy this over and explain to you what this code is doing. So we have a for loop now, that is going to go through every key within our property dict object, then we're going to have a print statement that has blank was bought in blank, total rental income is blank a month, we have enter string format, so we can enter in our variables that are going to go in each spot. Now because we're iterating through our dictionary, we specify what key we're going to select from. And then we're selecting the second key. In each case, we have bought on and rent income, go into playlists. And now we have an output that states 123 Main Street, which was our key was bought in 2019, January 1, which is key and then we had the second key which was bought on and total rental income is 750 a month because we specified rent income. This will make a lot more sense as we continue on our journey with using real world examples. What I want you to grasp here is that dictionaries allow us to hold our values, we can iterate through them to then pull out the information. As a recap, we cover three ways to store our data. Of these three ways. The two that we're going to use the most are lists and dictionaries. Once we go into loops, we'll have a really good understanding of how we can access each of these elements and modify them a bit. Let's explore these further in the following lesson. want full access to the introduction to real estate Data Analytics course. Then sign up for the course in the link below. You will learn Python programming all with real estate related examples. This includes web scraping, retrieving data from sources like Zillow, realtor, Redfin, Yahoo Finance, US census, and more. If you haven't already, check out the introduction video to the course on YouTube to get a full understanding of what the course has to offer. Also, members of the free tech and real estate group on Facebook receive a 20% off the course seeing the next lesson.

Transcribed by https://otter.ai

Previous
Previous

Python Conditionals and For Loops | Real Estate Analytics

Next
Next

Python Data Types | Real Estate Analytics