Suppose I want to create a list of the input
3 4 5 6 7
without the spacings. Is there a technical difference between list(map(int, input().split())) and list(map(int,input().replace(' ','')))?
I am inputting a list of this sort for a question on HackerRank. The split() version seems to always work, but the replace(' ','') version seems to only work for short length inputs.
list(map(int,input().replace(' ',''))) will fail if your numbers have more than two digits since it converts each character to an int.
Example:
>>> inp = '1 2 3 50'
>>> list(map(int, inp.replace(' ','')))
[1, 2, 3, 5, 0]
>>> list(map(int, inp.split()))
[1, 2, 3, 50]
For the same reason, it also isn't able to handle negative values or floats.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With