Creating a function for this exercise

Screen Link:

My Code

def uppercase(dataset, index):
    for row in moma:
        value= row[index]
        value= value.title(value)
        if not value and:
            value= 'Gender Unknown/Other'

Now I wanted to create a function to this exercise a bit easier, but I am getting stuck when it comes to the 2nd requirement of the exercise which is:

Clean the Nationality column of the data set (found at index 2) by repeating the same technique you used for the Gender column.
For missing values in the Nationality column, use the string “Nationality Unknown”.

I do not know how to continue the function in order to account for the Nationality column, because it needs to update with "Nationality Unknown".

Thanks a lot for your help, dear scientists! Happy coding

Can you provide the mission link to the problem? Thanks a lot.

For missing values in the Nationality column, use the string “Nationality Unknown”.

The problem statement tells us to replace any missing values in Nationality column at index 2 with string "Nationality Unknown".

if not row[index]:
    row[index] = "Nationality Unknown"

From PEP 8, in the “Programming Recommendations” section:

For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

To check if a string is empty

if not some_string:

To check if a string is not empty

if some_string:

Just to clarify, sequences are evaluated to False or True in a Boolean context if they are empty or not. They are not equal to False or True.

Hey There!

Here is the link. You will get a better understanding of where I got stuck in my thinking process when you will read the entire requirements for the exercise.

What I was following with my function was to be able to use both for Nationality and for Gender columns of the data

Here’s my code

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


for each row in moma

row[5] assigned “Gender Unknown/Other” if row[5] is empty; Otherwise, capitalised row[5].

row[2] assigned “Nationality Unknown” if row[2] is empty; Otherwise, capitalised the row[2].


for row in moma:
   if not row[5]:
        row[5] = "Gender Unknown/Other"

   if not row[2]:
        row[2] = "Nationality Unknown"

Clean column

Let’s reorganised the instruction.

Clean the Gender column.

Gender column, at index 5

Make the changes Gender column

  • Use the str.title() method to make the capitalization uniform.
  • Use an if statement to check if the value is an empty string. If the value is an empty string, give it the value "Gender Unknown/Other"

row[5] is column "Gender".

Left Hand Side = Right Hand Side

Left Hand Side is the global variable we want to updated.

Right Hand Side is the step we take to clean the global variable.

The end result, we want Left Hand Side to be updated row[5].

row[5] = = do cleaning = end result.

Broken down into smaller steps.

  • Assign the value from the Gender column, at index 5, to a variable.

value is a local temporary variable with life span until each iteration of the loop ends.

value = do the cleaning = not end result because value is local variable

We need to reassign the value back to row[5]. How else does the caller knows what local variable value holds at the end of the loop. Local variable value scoped or life span is until the end of each iteration of loop.

row[5] = value after cleaning for end result

Technically, we can replace the temporary variable value step and do the assignment onto the global variable we are interested in changing.

row[5] = do the cleaning for end result.

Eliminate the need of local variable for readable code.

Eliminate the need of local variable to avoid confusing instructions.

Eliminate the need of local variable to avoid logical errors.

1 Like

Alright so it is better to use a for loop instead of building a function like I wanted to do

1 Like

You may use a function.

def update_column(row, i, default): 
     row[i] = default if not row[i] else row[i].title()

for row in moma:
     update_column(row, 5, "Gender Unknown/Other")
     update_column(row, 2, "Nationality Unknown")
1 Like

Mhhhm, I think I need to get rid of this Einstellung effect on my thinking and learn to pivot a bit more with my coding.

My logic when I saw the exercise was to create a single function that will solve the exercise such as update_column(moma, 5) (like my initial example) and then I will have it solved. But I see that I can also create a function on the side and use it inside a loop.

As can be clearly seen, I am a beginner in this. Thank you so much @alvinctk for your time. Greatly appreciate it!

1 Like