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 trialMatthew Hayes
1,791 PointsVery smelly code improvements?
while writing code for this question I couldn't help but feel like I've gone along route around solving it. for example when I formated the return value and changing int/float into 'str'. I feel as though I've missed a few easy steps in simplifying my code. Can anyone critique it or suggest alternative methods? Thanks,
def combiner(alist):
stringjoin = ''
str_list = []
num_list = []
for item in alist:
if isinstance(item, str):
str_list.append(item)
elif isinstance(item, (int, float)):
num_list.append(item)
total = str(sum(num_list))
str_list = stringjoin.join(str_list)
return ("{}{}".format(str_list, total))
1 Answer
KRIS NIKOLAISEN
54,972 PointsGood job. You could make the code shorter by not using lists. Then the final results are handled during the iteration with no need for separate lines to sum and join the lists.
def combiner(alist):
str_list = ''
num_list = 0
for item in alist:
if isinstance(item, str):
str_list += item
elif isinstance(item, (int, float)):
num_list += item
return ("{}{}".format(str_list, str(num_list)))