Home > database >  How to get rid of the single quotation marks in a list inside a list?
How to get rid of the single quotation marks in a list inside a list?

Time:10-27

I wrote this piece of code, in which I ask the user to input numbers twice, and then it joins them together in a list, inside a list. It works...buts it's in the wrong format.

Here's what I did

list=[]
for i in range(2):
    userinput = input("Enter some numbers: ")
    x = userinput.split()
    list.append(x)
print(list)

The output is this

Enter some numbers: 1 2
Enter some numbers: 3 4
[['1', '2'], ['3', '4']]

...but I need it to be formatted [[1, 2], [3, 4]] I'm very new python, and I feel like Ive exhausted all my options, I couldnt find anything online that works (tried the join, replace etc.) , unless its impossible with my code, please help

CodePudding user response:

Use x = [int(i) for i in userinput.split()]; you have to convert the input strings to integers.

CodePudding user response:

You'll need to convert each value to an int to avoid the quotes (the values entered by the end user are strings):

list=[]
for i in range(2):
    userinput = input("Enter some numbers: ")
    x = userinput.split()
    list.append([int(v) for v in x])
print(list)

Output:

Enter some numbers: 3 2 1 
Enter some numbers: 4 5 6
[[3, 2, 1], [4, 5, 6]]

CodePudding user response:

You can do it in one line like below :

input_list = [int(input("Enter some numbers: ")) for i in range(2)]
  • Related