I'm trying to put user input (just a list of ints) into a list that already exists with one element in it. I'm not sure if it's possible to have a list running off of one element in an already existing list. Might eventually add more elements to already existing list.
Code below:
days = ["Monday"]
days[0] = [int(x) for x in input("Please enter your schedule: ").split()]
print(days)
I expected the results to give me a list within a list, but the actual result was:
days[0] = [int(x) for x in input("Please enter your schedule: ").split()]
ValueError: invalid literal for int() with base 10: '1000,'
1 Answer 1
You can do this:
days = ["Monday"]
days.append( [int(x) for x in input("Please enter your schedule: ").split()] )
print(days)
That'll give you ["Monday", [1000, 2000, 3000]] if you provided 1000 2000 3000 from command prompt.
If you do this:
days = ["Monday"]
input_data = input("Please enter your schedule: ")
split_data = input_data.split()
for item in split_data:
days.append(item)
print(days)
You will get ["Monday", 1000, 2000, 3000]
Or you can use dictionary like so:
days = {}
days["Monday"] = [int(x) for x in input("Please enter your schedule: ").split()]
print(days)
to get {'Monday': [1000, 2000, 3000]}
1000 2000 3000or such with spaces.1000 2000 3000at command prompt?