Not clear with why an index is needed for this exercise

Screen Link:

My Code:

header = new_line_split[0]
data_row_1 = new_line_split[1]
data_row_2 = new_line_split[2]
first_three = [header, data_row_1, data_row_2]

for i in first_three:
    split = i.split(",")
    print(split)

What I expected to happen:
I would expect that the code below ‘#My version’ gives the same output as the code given in the example. I am not clear with this line of code given in the example. Why do we need the index, if with each_string we already specify the element within the loop?

    first_three[index] = each_string.split(",")

What actually happened:

['id', 'track_name', 'size_bytes', 'currency', 'price', 'rating_count_tot', 'rating_count_ver', 'user_rating', 'user_rating_ver', 'ver', 'cont_rating', 'prime_genre', 'sup_devices.num', 'ipadSc_urls.num', 'lang.num', 'vpp_lic']
['284882215', 'Facebook', '389879808', 'USD', '0.0', '2974676', '212', '3.5', '3.5', '95.0', '4+', 'Social Networking', '37', '1', '29', '1']
['389801252', 'Instagram', '113954816', 'USD', '0.0', '2161558', '1289', '4.5', '4.0', '10.23', '12+', 'Photo & Video', '37', '0', '29', '1']

Enter other details below
For me the following two lines within the loop return the same thing:

index = 0
for i in first_three:
    print(i)
    print(first_three[index])

Hence, I am not understanding what we are doing here:

    first_three[index] = each_string.split(",")
1 Like

Hi mr.vincenzo.galante

Please provide the screen link, so that it would help others to see the lesson and understand the context.

3 Likes

Thanks for the hint. I added it.

Thanks for the edit.

Yes, but it is not updating the first_three variable. The instruction states that:

  1. Use a for loop to modify first_three so that its elements are the first three rows (in order) split on the comma character.

If you use a print statement before and after the for loop, you could see the differeence.

header = new_line_split[0]
data_row_1 = new_line_split[1]
data_row_2 = new_line_split[2]
first_three = [header, data_row_1, data_row_2]
print('Before for loop')
print(first_three)
index = 0
for each_string in first_three:
    first_three[index] = each_string.split(',')
    index += 1
print('After for loop')
print(first_three)

The output is

Before for loop
['id,track_name,size_bytes,currency,price,rating_count_tot,rating_count_ver,user_rating,user_rating_ver,ver,cont_rating,prime_genre,sup_devices.num,ipadSc_urls.num,lang.num,vpp_lic', '284882215,Facebook,389879808,USD,0.0,2974676,212,3.5,3.5,95.0,4+,Social Networking,37,1,29,1', '389801252,Instagram,113954816,USD,0.0,2161558,1289,4.5,4.0,10.23,12+,Photo & Video,37,0,29,1']
After for loop
[['id', 'track_name', 'size_bytes', 'currency', 'price', 'rating_count_tot', 'rating_count_ver', 'user_rating', 'user_rating_ver', 'ver', 'cont_rating', 'prime_genre', 'sup_devices.num', 'ipadSc_urls.num', 'lang.num', 'vpp_lic'], ['284882215', 'Facebook', '389879808', 'USD', '0.0', '2974676', '212', '3.5', '3.5', '95.0', '4+', 'Social Networking', '37', '1', '29', '1'], ['389801252', 'Instagram', '113954816', 'USD', '0.0', '2161558', '1289', '4.5', '4.0', '10.23', '12+', 'Photo & Video', '37', '0', '29', '1']]

You could use print in your code as well and see the difference.
Hope this is clear now. Let know if you have any further doubts.

Thanks.

2 Likes

Understood. So basically we are re-assigning the variables within the list, with a new value. We are replacing them with the modified version of the .split. Thanks for explaining it.

3 Likes