Home > Mobile >  Why does my list have extra double brackets?
Why does my list have extra double brackets?

Time:11-03

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.

CodePudding user response:

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

CodePudding user response:

Use:

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

CodePudding user response:

You're appending a list to arr_list.

Use this instead:

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

CodePudding user response:

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

CodePudding user response:

Use

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

or

print(''.join(arr[0:4]))
  • Related