krubsburger
krubsburger

Reputation: 91

How to remove empty lines from a nested list?

I call data from Google Sheets, I get an answer in the form of a dictionary with a nested dictionary, since I have two strings combined, the response from the server returns empty strings, how can I get the edited response and write it to a new list?

{'majorDimension': 'COLUMNS',
 'range': "'Sheet1'!A3:A964",
 'values': [['Sticker | Fnatic | 2020 RMR',
             '',
             'Sticker | Fnatic (Holo) | 2020 RMR',
             '',
             'Sticker | FaZe | 2020 RMR',
             '',
             'Sticker | FaZe (Holo) | 2020 RMR',
             '',
             'Sticker | TYLOO | 2020 RMR',
             '',
             'Sticker | TYLOO (Holo) | 2020 RMR',
             '',
             'Sticker | G2 | 2020 RMR',
             '',
             'Sticker | G2 (Holo) | 2020 RMR',
             '',
             'Sticker | Ninjas in Pyjamas | 2020 RMR',
             '',
             'Sticker | Ninjas in Pyjamas (Holo) | 2020 RMR',
             '',
             'Sticker | Astralis | 2020 RMR',
             '',
             'Sticker | Astralis (Holo) | 2020 RMR',
             '',
             'Sticker | Natus Vincere | 2020 RMR',
             '',
             'Sticker | Natus Vincere (Holo) | Katowice 2015']]}

I need:

new_list =
['Sticker | Fnatic | 2020 RMR',
 'Sticker | Fnatic (Holo) | 2020 RMR',
 'Sticker | FaZe | 2020 RMR',
 'Sticker | FaZe (Holo) | 2020 RMR',
 'Sticker | TYLOO | 2020 RMR',
 'Sticker | TYLOO (Holo) | 2020 RMR',
 'Sticker | G2 | 2020 RMR',
 'Sticker | G2 (Holo) | 2020 RMR',
 'Sticker | Ninjas in Pyjamas | 2020 RMR',
 'Sticker | Ninjas in Pyjamas (Holo) | 2020 RMR',
 'Sticker | Astralis | 2020 RMR',
 'Sticker | Astralis (Holo) | 2020 RMR',
 'Sticker | Natus Vincere | 2020 RMR',
 'Sticker | Natus Vincere (Holo) | Katowice 2015']

Upvotes: 1

Views: 72

Answers (2)

nabroleonx
nabroleonx

Reputation: 96

You can filter it out using filter()

new_list = list(filter(None, c['values'][0]))

Upvotes: 2

Rustam Garayev
Rustam Garayev

Reputation: 2692

Simple list comprehension with checker will do the job (assuming data is the name of variable that the response is being stored):

new_ls = [each for each in data['values'][0] if each != '']

new_ls would look like:

['Sticker | Fnatic | 2020 RMR', 'Sticker | Fnatic (Holo) | 2020 RMR', 'Sticker | FaZe | 2020 RMR', 'Sticker | FaZe (Holo) | 2020 RMR', 'Sticker | TYLOO | 2020 RMR', 'Sticker | TYLOO (Holo) | 2020 RMR', 'Sticker | G2 | 2020 RMR', 'Sticker | G2 (Holo) | 2020 RMR', 'Sticker | Ninjas in Pyjamas | 2020 RMR', 'Sticker | Ninjas in Pyjamas (Holo) | 2020 RMR', 'Sticker | Astralis | 2020 RMR', 'Sticker | Astralis (Holo) | 2020 RMR', 'Sticker | Natus Vincere | 2020 RMR', 'Sticker | Natus Vincere (Holo) | Katowice 2015']

Upvotes: 1

Related Questions