Get a list of numbers as input from the user

0 votes

I tried to use input (Py3) /raw_input() (Py2) to get a list of numbers, however with the code

numbers = input()
print(len(numbers))

the input [1,2,3] and 1 2 3 gives a result of 7 and 5 respectively – it seems to interpret the input as if it were a string. Is there any direct way to make a list out of it? Maybe I could use re.findall to extract the integers, but if possible, I would prefer to use a more Pythonic solution.

Feb 17 in Python by Nandini
• 5,480 points
64 views

1 answer to this question.

0 votes
value = [int(num) for num in input().split()]

Output

1 2 3  4
value

Output

[1, 2, 3, 4]
print(len(value))
4

Another Way:

solution = input()
1 2 3 4 5 6 7 
numbers = list(map(int, solution.split()))
numbers
Output
[1, 2, 3, 4, 5, 6, 7]
print(len(numbers))
7
answered Feb 17 by Dev
• 6,000 points

Related Questions In Python

0 votes
2 answers

How can I get the count of a list in Python?

n=[1,2,3,4,5,6,7,8,9] print(len(n)) =9 READ MORE

answered Dec 10, 2020 in Python by anonymous
775 views
0 votes
0 answers
+1 vote
1 answer

how do i get a optimal cutoff value(directly in the form of numeric) from roc curves?

The optimal CutOff value is the point ...READ MORE

answered Oct 17, 2019 in Python by Saira
2,336 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,145 views
0 votes
1 answer
0 votes
1 answer

How do I get the number of elements in a list

items = [] items.append("apple") items.append("orange") items.append("banana") len(items) #use the ...READ MORE

answered Feb 15 in Python by Dev
• 6,000 points
83 views
0 votes
1 answer

How can I capitalize the first letter of each word in a string?

By using  the .title() method of string ...READ MORE

answered Feb 17 in Python by Dev
• 6,000 points
104 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP