how can i input multiple numbers or strings only at once to append them to a list?

57 views Asked by At

I need to repeat the process of inputting the numbers as many as mentioned in the variable number:

number = int(input("How many numbers do you want in your list? "))
list = [for i in range(0,number)]
2

There are 2 answers

0
Moinuddin Quadri On

You may simply do:

my_list = [int(input("Enter number {}... ".format(i+1))) for i in range(0,number)]

Here, my_list will hold the list of n numbers entered by the user.

Note: list is built-in type in Python. Do not use it as variable name

0
sporkl On

If you want to have a bunch inputted at once, separated by commas, you could use:

w = input("Enter stuff to add to the list, separated by commas.")
my_list = w.split(",")
print(my_list)

This code accepts input as comma-separated stuff like this: a, 1, @^ and turns it into this: ['a', '1', '@^'] The code works by accepting comma-ed input as w, then splitting it into a list by the commas, using the built in split() method.