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

Question:

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

Asked By: somebody5050403

||

Answers:

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

Answered By: RJ Adriaansen

You can do it in one line like below :

input_list = [int(input("Enter some numbers: ")) for i in range(2)]
Answered By: soumya-kole

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]]
Answered By: Mark
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.