0

I have a list which I get from reading gsheet, it has all values in square brackets like this:

[['example1'], ['example2'], ['example3'], ['example4'], ['example5']]

I would like to remove brackets from values for further steps and it should look like:

['example1', 'example2', 'example3', 'example4', 'example5']

I'm stuck here for a while, any help is appreciated. Bit of my code:

    SOURCE_SHEET = CLIENT.open_by_url(SOURCE_G_SHEET_URL).worksheet(source_ws_title)
    rows_read = SOURCE_SHEET.get_all_records()
    df_raw = pd.DataFrame(rows_read)
    list = df_raw.values.tolist()
tagilas
  • 65
  • 8
  • As side note: avoid using `list` as variable name, as this overshadow `list` built-in – Daweo Jun 07 '21 at 08:36
  • 1
    As another side note: I assume you want to load a worksheet from Google Sheets to a Pandas Dataframe. I really like this function of Pygsheets which does exactly that: https://pygsheets.readthedocs.io/en/stable/worksheet.html#pygsheets.Worksheet.get_as_df – Rutger Jun 07 '21 at 08:37

2 Answers2

1

What you need is to extract every element from the inner list and put it in the main list, something like this:

[j for i in init_list for j in i]

How does it work? Think of it as a for look:

final_list = []
for i in init_list:
    for j in i:
        final_list.append(j)

Another way to do is using numpy's squeeze function.

x = np.array(init_list)
np.squeeze(x)
paradocslover
  • 2,932
  • 3
  • 18
  • 44
0

If every list in the list has only one record, you can do this list comprehension as well:

[i[0] for i in initial_list]
Rutger
  • 593
  • 5
  • 11