0

input comma separated numbers, convert them into integer from string and then into integer list. this is what i came up with. is there any other way?

x = list(map(lambda x : int(x),(input()).split(",")))
print(x)

input : 1,2,3,55,66,714,78

output : [1, 2, 3, 55, 66, 714, 78]

Code-Apprentice
84k26 gold badges163 silver badges290 bronze badges
asked Aug 23, 2022 at 5:47
6
  • As your code works, I would suggest this may belong on codereview.stackexchange.com. Commented Aug 23, 2022 at 5:55
  • 6
    However, I would also suggest: list(map(int, input.split(','))) Commented Aug 23, 2022 at 5:55
  • As alluded to by @Chris, the lambda is not needed as map maps each element in the list to int. So answer your question, yes - there are many ways. But this is generally the most efficient. Commented Aug 23, 2022 at 5:59
  • What is your question? Commented Aug 23, 2022 at 6:01
  • 1
    Thanks for catching my late night oops missing the (). Commented Aug 23, 2022 at 6:08

2 Answers 2

4

there's also list comprehension

[int(n) for n in input().split(",")]
answered Aug 23, 2022 at 5:50
Sign up to request clarification or add additional context in comments.

1 Comment

List comprehension would be slightly faster than list(map())
0

If you don't mind a tuple, you can use eval

eval(input())
1,2,3,55,66,714,78
(1, 2, 3, 55, 66, 714, 78)
answered Aug 23, 2022 at 6:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.