LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

HELP ! Why is it important to assign back?

Screen Link:
https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/5/string-capitalization

My Code:

for row in moma:
    Gender = row[5]
    Gender = Gender.title()
    if not Gender:
        Gender = 'Gender Unknown/Other'

    
    Nationality = row[2]
    Nationality = Nationality.title()
    if not Nationality:
        Nationality = 'Nationality Unknown'

    
moma[:5]        

What I expected to happen:

I expect it to take the latest value automatically. But its showing an error

What actually happened:

-  ['Untitled', 'Unknown', '', '', '', '', 'c. 1925', 'Photography'],
+  ['Untitled',
+   'Unknown',
+   'Nationality Unknown',
+   '',
+   '',
+   'Gender Unknown/Other',
+   'c. 1925',
+   'Photography'],
if I run the code below after assigning the values back to the row I don't get any error. In short could some one walk me through the process of how this loop works and why assigning back to the row is important?

Code which shows no error

for row in moma:
    Gender = row[5]
    Gender = Gender.title()
    if not Gender:
        Gender = 'Gender Unknown/Other'
    row[5] = Gender
    
    Nationality = row[2]
    Nationality = Nationality.title()
    if not Nationality:
        Nationality = 'Nationality Unknown'
    row[2] = Nationality
    
moma[:5]        

THANK YOU IN ADVANCE :heart: :heart:

If you have the following -

a = [4, 5, 6]

and

b = a[0]

If I want to update a to [5, 5, 6], how would I do that?

If I do the following -

b = b + 1

Would that update both a and b or would that update only b?

Think about the above, and then think about why assigning back to row is required. Focus on the variables, the values, and what gets updated when.

1 Like

Thanks for explaining it in a way that required my thinking. I think I got it. as ‘a’ won’t get updated hence its important to assign back here.

1 Like