Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove square brackets for each item from a list [duplicate]

Excel has a list of numbers, I am expecting to see something like this:

[110652450, 5309154]

However I see something like this:

[[110652450], [5309154]] 

I tried newtest = [x[:-1] for x in df] suggested on a similar question but that just removes my numbers and keeps the braquets

df = pd.read_excel(filename.xlsx",sheetname='Sheet1')


df = df.values.tolist()

print(df)
like image 707
pepperjohn Avatar asked Aug 31 '25 22:08

pepperjohn


2 Answers

I didn't quite get your question, but you seem to be looking for something like this?

df = [[110652450], [5309154]]
newest = [i[0] for i in df]
print(newest)
>> [110652450,5309154]
like image 144
Mr. T Avatar answered Sep 03 '25 23:09

Mr. T


numpy.squeeze might be the preferred method to do this. Call np.squeeze(a) for the array that you wish to flatten. np.squeeze should be much faster than a list comprehension such as [e[0] for e in a]

like image 36
swhat Avatar answered Sep 04 '25 00:09

swhat