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 trialLuis Gutierrez
Python Development Techdegree Student 2,336 Pointshow can you fix an integer?
def x(data): cleaned = [] for user in data: fixed = {} fixed["int"]?
1 Answer
Travis Alstrand
Treehouse Project ReviewerHi Luis Gutierrez 👋
Without any context, I'm not really sure what it is you mean by this, are you trying to change a string of a number into an integer and store it in a dictionary? Then append that dictionary to a list?
If so, you can utilize int()
to change a string number to an integer type. For example...
num_string = "37"
cleaned_num = int(num_string)
print(type(cleaned_num))
This should show <class 'int'>
in the terminal after running.
I'm not sure what data
is being passed in but from the loop I assume it's some sort of iterable collection of "users".
def x(data): # is data a list of user name strings? strings of integers? a dictionary?
cleaned = [] # empty list I assume we'll append these new 'fixed' dictionaries to and return
for user in data:
fixed = {} # new dictionary I assume we'll add to `cleaned`
fixed["int"]? # this would be setting a Key in this `fixed` dictionary by the name of `int` but no value
To assign a Value to the fixed
dictionary's "int"
Key, you'll want to simply use the assignment operator =
with whatever logic that entails.
fixed["int"] = int(user)
Without knowing what data
is, I just threw this together as an example, I hope this helps, if not, please clarify what it is you're working with and what it is you're trying to achieve.
def clean_data(data):
cleaned = []
for num_string in data:
fixed = {}
fixed["int"] = int(num_string)
cleaned.append(fixed)
for num in cleaned:
print(num)
return cleaned
num_data = ["1", "24", "37"]
cleaned_numbers = clean_data(num_data)
This will print the following to the terminal
{'int': 1}
{'int': 24}
{'int': 37}