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 DRY

Alternative?

Instead of having 2 functions with separate for loops, I have 1 function that loops through the lists and changes the price and adds a location at the same time. Is this okay? Or would the 2 separate functions be preferred since my single function is technically doing 2 tasks, even though its through the same iteration of a list?

def change_list(list):
    for item in list:
        item['price'] = float(item['price'])
        item['location'] = 'produce section'

change_list(fruits)
change_list(vegetables)

1 Answer

Megan Amendola
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

Totally! It really depends on your code and what you are doing with the fruits and veggies. If either one ever changes, like you add ['region'] to say the fruits and not to the vegetables, it might break your single function and two functions might be more manageable. If the plan is to have both fruits and veggies always be the same structure, then a single function would always work for both and you would only need to update the one function if something changes. It's all a part of planning out your code from the start and then refactoring it as your plans change. Great question!