Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Solution

question on best practice

my solution worked, but i'm not sure if it's "best practice". should i be passing the data as an argument into the function call and adding a parameter in the function definition instead? this was my solution:

from data import data


def splitNames():
    for dic in data:
        nameSplit = []
        nameSplit = dic["name"].split()
        dic["firstName"] = nameSplit[0]
        dic["lastName"] = nameSplit[1]
        del dic["name"]
    return data

def adminBool():
    for dic in data:
        if dic["admin"] == 'False':
            dic["admin"] = False
        elif dic["admin"] == 'True':
            dic["admin"] = True
    return data

def idConversion():
    for dic in data:
        dic['id'] = int(dic['id'])
    return data

dataMod = []
dataMod = splitNames()
dataMod = adminBool()
dataMod = idConversion()
print(dataMod)

1 Answer

Steven Parker
Steven Parker
231,236 Points

Passing data as an argument is probably more common, but if the function never needs to respond to different data it's not really an issue.

However, when an attribute name is known in advance, it's generally better to use membership notation (like dic.admin) instead of bracket notation(like dic["admin"]).

Ok, maybe I'll get into habit of passing argument. Good to know about membership notation too, I forgot or never knew that. Thanks!