Skip to content
← Back to Community
Hi. Me again
Profile icon
FARHEENPALAGIRI

Hey everyone. I have a not-so-slight issue. So for this code I'm writing, a three digit number is inputted, and if the digits are in ascending order the code outputs YES and if it is not, the code outputs NO. So I was trying to make the number into a list, do the thing where a variable was assigned to each item on the list, and then find out if the digits are in ascending order using an if/else statement, but then I realized my list was in string form. So, I tried to convert to integer form. I did this:

a = int(input())
num_list = list(str(a))
num_list_2 = map(int, num_list)
print(num_list_2)

But when I tried the print statement, I got an output of:

<map object at 0x7f1174cd6390>

What does this even mean? It's not even a syntax error. Does anyone know another way to turn the string list into an integer list? Please help.

Voters
Profile icon
FARHEENPALAGIRI
Comments
hotnewtop
Profile icon
Geocube101

You could just get the input and send it to an array: a = list(input()) which should return the input as a list with each character as an element (You then need to iterate through and convert each element to an integer via int() method). From there, you can make another list and call the sorted() function to sort it. Compare the first list with the second sorted list and done! If they match, it should return True.

a = list(input())
for b in range(len(a)):
--a[b] = int(a[b])
c = sorted(a)
if a == c:
--print(True)
else:
--print(False)

#Dashed are representative of spaces

Profile icon
FARHEENPALAGIRI

@Geocube101 Thank you so much

Profile icon
ash15khng

You could do something like
num_list_2 = [int(num) for num in num_list]
This is called a list comprehension and is basically a shorter way of writing

num_list_2 = [] for num in num_list: num_list_2.append(num)

If you want to use your code, change line 3 to num_list_2 = list(map(int, num_list)). map() returns a map object, not a list, so you need to turn in into a list by adding list() in front of it.

Profile icon
FARHEENPALAGIRI

@ash15khng Thanks a lot. So an array is like a list for integers, right?

Profile icon
HappyFakeboulde

AFAIK map doesn't return a list, it returns an iterator.
I could be wrong though 🤷.

Profile icon
FARHEENPALAGIRI

@HappyFakeboulde What's an iterator??

Profile icon
HappyFakeboulde
Profile icon
FARHEENPALAGIRI