On Sat, May 4, 2013 at 12:13 AM, Jim Mooney <cybervigila...@gmail.com> wrote: > I'm turning an integer into a string so I can make a list of separate > chars, then turn those chars back into individual ints, but the > resulting list still looks like string chars when I print it. What am > I doing wrong? > > listOfNumChars = list(str(intNum)) > for num in listOfNumChars: > num = int(num) > > print(listOfNumChars) > > # result of 455 entered is ['4', '5', '5']
The body of your for loop only rebinds the loop variable. It's not appending to a new list or modifying listOfNumChars. As to the latter list, it's redundant since a string is iterable. The following snippet creates the list [4, 5, 5]: num = 455 numlist = [] for c in str(num): numlist.append(int(c)) or using a list comprehension: numlist = [int(c) for c in str(num)] or using map: numlist = list(map(int, str(num))) iterators http://docs.python.org/3/library/stdtypes.html#typeiter built-in functions http://docs.python.org/3/library/functions.html#iter http://docs.python.org/3/library/functions.html#map for statement http://docs.python.org/3/reference/compound_stmts.html#the-for-statement comprehensions http://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor