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

Splitting multiple strings within a dictionary that is nested within a list.

Having some issues getting this function to do what I want it to do. Essentially what I'd like to do is have my "clean_heights" function to import all the height keys from a dictionary nested within a list. Once I achieved iterating and importing over all the heights, I added them to a list called "player_heights". When called, it prints all heights in strings. I need to turn these strings into integers, but first, I must iterate through my list and split the strings, and this is where I'm stuck.

Here's the error I'm running into: "AttributeError: 'list' object has no attribute 'split'"

def clean_height(height_list):
    for heights in height_list:
        player_heights = [heights['height']].split()

        print(player_heights)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Perhaps a list comprehension will work for you:

for heights in height_list:
        player_heights = [heights['height']].split()
# if heights['height'] contains single value
def clean_height(height_list):
    player_heights = [heights['height']
                      for heights in height_list]
    return player_heights

# if heights['height'] contains list of values
def clean_height(height_list):
    players_heights = []
    for heights in height_list:
        player_heights.extend(heights['height'])
    return player_heights

Post back if you need more help, Good luck!!