1

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,'
7
  • When you were asked to enter your schedule, what did you type? If you typed commas, don't type that. Just type 1000 2000 3000 or such with spaces. Commented Apr 21, 2019 at 20:20
  • So I did take out the commas from the input but I'm also trying to have Monday included in the array when it's printed out, so I changed days[0] to days[1] but now it's throwing me this error: IndexError: list assignment index out of range I could try appending things, but I'm not sure how this works. Commented Apr 21, 2019 at 20:25
  • What do you want days to contain after you provide information like 1000 2000 3000 at command prompt? Commented Apr 21, 2019 at 20:27
  • I want days to look like ["Monday" [1000, 2000, 3000]] if that's even possible? Commented Apr 21, 2019 at 20:28
  • You will get ["Monday", [1000, 2000, 3000]] - notice the comma after "Monday". That works for you? Commented Apr 21, 2019 at 20:29

1 Answer 1

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]}

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I believe the last one is more optimal and closer to what I was looking for.

Your Answer

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.