Why does my list have extra double brackets?

Question:

arr_list = []
arr = ['5', '6', '2', '4', '+']

arr_list.append([''.join(arr[0:4])])
print(arr_list)

Ouput: [['5624']]

Why does the output have 2 sets of square brackets? I only want one.

Thanks in advnace.

Asked By: Benjamin McDowell

||

Answers:

Use:

arr_list.append(''.join(arr[0:4]))
Answered By: brenodacosta

You’re appending a list to arr_list.

Use this instead:

arr_list.append(''.join(arr[0:4]))
Answered By: helloworld
arr_list = []
arr = ['5', '6', '2', '4', '+']
arr_list.append(''.join(arr[0:4]))
print(arr_list)

simply remove the brackets from the third row

Answered By: Tr3ate

Remove the brackets around what you are appending:

>>> arr_list = []
>>> arr = ['5', '6', '2', '4', '+']
>>> arr_list.append(''.join(arr[0:4]))
>>> arr_list
['5624']

Or you could use list.extend rather than list.append:

>>> arr_list = []
>>> arr = ['5', '6', '2', '4', '+']
>>> arr_list.extend([''.join(arr[0:4])])
>>> arr_list
['5624']
Answered By: Sash Sinha

Use

arr_list.append(''.join(arr[0:4]))

or

print(''.join(arr[0:4]))
Answered By: OneLastBug
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.