21. Convert numerical values index doubt

Hi sir/ma’am, in the loop its written row[1] which according to the table is Casey 176544.328149. But we have to convert only 176544.328149 this into float as per the question, then why we have selected the entire row. if we have to convert columns then why row is written. Not getting.
type(row[0][1]) means in row 0, first element i.e Estimated column? correct me and guide me. Thanks in advance.

Screen Link:
https://app.dataquest.io/m/1009/lists-and-for-loops-practice-problems/21/convert-numerical-values

My Code:

rows = all_rows[1:]
for row in rows:
    row[1] = float(row[1])
    
print(rows[:5])
type(row[0][1])

In the for loop, print out row, row[0], and row[1]. See what the output of the print statements are, and you will start to understand better what is being selected and why.

You will also be able to answer this question when you print out the values I suggested above.

if i print row i am getting,
[‘Casey’, 176544.328149]
[‘Riley’, 154860.66517300002]
[‘Jessie’, 136381.830656]
[‘Jackie’, 132928.78874000002]
[‘Avery’, 121797.41951600001]
[‘Jaime’, 109870.18729000002]
[‘Peyton’, 94896.39521599999]
[‘Kerry’, 88963.92625]
[‘Jody’, 80400.519199]
[‘Kendall’, 79210.873961]
and so on

if i print row[0],
Casey
Riley
Jessie
Jackie
Avery
Jaime
Peyton
Kerry
Jody
and so on

if i print row[1],
176544.328149
154860.66517300002
136381.830656
132928.78874000002
121797.41951600001
109870.18729000002
94896.39521599999
88963.92625
80400.519199
79210.873961
64151.630388
and so on

why is this represented in column form? if i printed row. i am still confused and struggling. So please give me a direct answer And explanation rather then telling me to fish for answer. thanks

after printing all this if i print row[0][1] it shows e. what is e? still i don’t understand what is row[0][1]. you can refer my question and tell me if i understood right or wrong.

also additional doubt if i print row outside for loop i get, [‘Aeon’, 100.21104]. curious how this works.

Direct answers are only helpful when you have broken down the problem yourself first. Breaking down the problem allows you to observe and understand patterns. Observing and understanding patterns allows you to create mental models to be able to solve more problems and debug any similar issues you might face in the future.

In a for loop, if you include a print statement, it will print out the content line-by-line for every iteration of that loop. That’s why it’s printing out the values line by line as you observe.

You printed out row, what did you get?

The first output -

[‘Casey’, 176544.328149]

What is this?

The first list in your list of lists.

You printed out row[0]. What did you get?

The first output -

Casey

Casey is the first item in the above list that you printed out.

How do you obtain an element from a list in Python?

Using indexing. In Python, indexing starts at 0. So, the first item is obtained using the index 0. That’s what row[0] returns.

Given the above information, what will row[0][1] return?

row returned the list above. row[0] returned the first item from that list, which was a string Casey. So, row[0][1] will return the item at index 1 in the string Casey.

What are the individual items in the string Casey?

“C”, “a”, “s”, “e”, “y”

Which item from the above is at index 1?

That’s a.

And that’s what you will get from row[0][1] when the row is [‘Casey’, 176544.328149]

Stop and Think

What would you get if you printed out row[1][3]? Think about the answer first. Then print it out in your code to cross-check if you got it right. This will help you ensure you understood the above properly.

There are multiple concepts here that you don’t currently seem fully comfortable with. I would recommend going through the courses/Missions that cover the Python basics around print statements and indexing again to improve your understanding. I would also recommend going through the basics of for loops and lists as well.

1 Like

ok got you but i was struggling since six days so. i print row[0][1] i am getting e instead of a. i mentioned this in above reply.
also if i print row[1][3] i should get 5 right? i visualized like you said but i am getting this
TypeErrorTraceback (most recent call last)
in ()
9
10 print(rows[:5])
—> 11 print(row[1][3])

TypeError: ‘float’ object is not subscriptable

And what is row here? If it’s not the same as [‘Casey’, 176544.328149] then you won’t get an a.

If you are trying to print row[0][1] outside of the for loop, then your row would be the last list in rows. Because your for loop completed all iterations, so row now has the last value only outside of that loop.

The last row in rows is ['Aeon', '100.21104', 4]. Therefore, as you can see, you will get an e from row[0][1] if you print outside of the for loop.

That’s a good question. You should get a 5 if row[1] is of the string datatype.

You can use type() to check the datatype of the value store. For example, print(type(row[1]))

As the error states, the datatype seems to be float in your case. Which is not subscriptable. That is, you can’t extract the individual numbers from that float using indexing. That’s only applicable to strings or elements in a list, for example.

The row[1] for each row in rows is of string datatype. And you need to convert them to float as per the problem statement. So, if you printed out row[1][3] before you converted it to float, you would have indeed obtained 5 for the ['Casey', '176544.328149'] row.

The above might be a lot of information to take in. So, go through it at your own pace.

2 Likes

Nope i got this reply of yours in seconds. I was already visualizing this as i have converted into float, the error is there, if it was a string like in the original question then i would’ve got 5 right? Anyway now i got what exactly row[0][1] means and why it gave output ‘str’ because row[0][1] inside for loop is a which is str.
Using the type() function, print the data type of the estimated number column in the first data row. this was the second question. So did we checked the right? As per this question shouldn’t we check row[1][2] something because the estimated column has index row[1].

Yes.

I am not sure of what you are asking here exactly.

“Using the type() function, print the data type of the estimated number column in the first data row”. This was the second question asked to test the answer, in which we did print(type(row[0][1]) but that gives us ‘a’ , right?. but here we are asked to check the data type of the estimated number column. which is this row - 176544.328149 so its index is [1] as per we discussed above. so thats what i was asking that shouldnt we check row[1][any index] rather then checking row[0][1] which gives us ‘name’ column and not the ‘estimated _number’ column as asked in the testing question.

No, it won’t give us a. You are checking for the datatype with type(). So, that would return the datatype of row[0][1].

You are confusing between row and rows.

type(rows[0][1]) (notice the s) will give you the datatype of the value in the estimated_number column for the first list (rows[0], notice the s here too). This is what they are asking to test the answer with.

type(row[1][any index]) will give you the datatype of the value at any index in row[1] where row would be a list in rows.

yeah sorry i meant, it will give us str. and without type it will give ‘a’.

yes i saw now that it is rows instead of what i wrote. so rows means the header row and in that [0][1] means the estimated number one, so it gave me float as answer when i checked the type, because we converted into float as per asked in the question. If we didnt convert than it would return str by default right? please tell me i got everything right now. if yes then Thank you for explaining everything to me and bearing with me. Thank you so much doctor.

1 Like

I am assuming that by “header row” you mean the actual list that stores ['name', 'estimated_number'].

If that’s what you mean by the “header row”, then no.

rows is your list of lists without the above list. rows is not your header row. Your header row is the first row of all_rows. rows does not include the header row.

You are converting the second value for each row in rows to float.

Yes, that’s correct.

1 Like

yes now i got it all rows is what we print excluding the header row. And we want the type for second value from the first list which is rows[0][1] which we converted from str to float. i took too much time to understand but got it. Thanks a lot :slight_smile:

1 Like

img

hi sir in the above conversation we discussed that to check the type we used type(rows[0][1]) to get the float value. So why in the image above we used app[1] to get the value of price? we can use app_and_price[0][1]? I am confused. Or when getting the float value in the above discussion we can use type(row[1]). Can you clear this for me? @the_doctor

Hi. Since this is not the same practice problem (or Mission) as your original question, please create a new, separate post for it.

but my doubt is related to this question. if i will create another post then you eventually have to come here to check what we discussed. To make your work easier i posted here becauseMY DOUBT IS IN CONTINUATION WITH OUR DICUSSION i will appreciate if you answer it here.

I answered the original question. And your new question can be answered by that same discussion.

In my previous replies to your original question I did two things -

  1. Ask you to print out values and focus on understanding what happens in your code.
  2. Break down the process, step-by-step, through asking yourself questions and answering those questions.

If you can follow the above two steps and be specific about why you are still confused given my previous responses to your original question, I can try to answer here. But based on what you have shared of your new problem, my previous responses should help you out.

If you don’t wish to be specific about it, that’s absolutely fine as well. But in that case, I would highly suggest that you create a new question with the appropriate link (to the practice problem or Mission) and details, and someone else from the community can surely help you out soon enough.

1 Like

sometimes you have to look from other people’s perspective, how can i post a new question if my doubt was related to this question? I have always followed rules while posting questions. Nvm i will try to google and understand if i can, because i don’t know i will create a new post and then ask you to come here and see the doubt, posting here was easy for YOU to review. I never said that i don’t want to be specific. Totally NOT satisfied with the community experience so far.

1 Like