0

Suppose that you have a list of strings like the following one:

the_list = ['02:00', ' GBP', '', 'Construction Output (MoM) (Jan)', '1.1%', '0.5%', '2.0%', '', '02:00', ' GBP', '', 'U.K. Construction Output (YoY) (Jan)', '9.9%', '9.2%', '7.4%', '', '02:00', ' GBP', '', 'GDP (MoM)', '0.8%', '0.2%', '-0.2%', '', '02:00', ' GBP', '', 'GDP (YoY)', '10.0%', '9.3%', '6.0%', '', '02:00', ' GBP', '', 'Index of Services', '1.0%', ' ', '1.2%', ''] 

Which every 8 elements appears '', which is an empty string, and it happens to be the same empty string that exists next to the one called GBP

How could the_list variable be updated so that it deletes the '' element that appears every 8 elements in the array of strings?

2
  • I just would like to know how can I delete the elements that are empty strings '' except those that are next to the elements set as ` GBP` @OrkhanAliyev Commented Mar 12, 2022 at 0:33
  • 2
    One thing to note: it looks like you have gathered multiple rows of data into a single list. You should consider building this as a 2D list or a list of dictionaries or class objects to begin with which might make it easier to massage the format later. Commented Mar 12, 2022 at 1:00

3 Answers 3

3

If I understood you correctly, you can use list-comprehension to filter out the values at specific index:

new_list = [word for idx, word in enumerate(the_list, 1) if idx % 8 != 0] print(new_list) 

Prints:

['02:00', ' GBP', '', 'Construction Output (MoM) (Jan)', '1.1%', '0.5%', '2.0%', '02:00', ' GBP', '', 'U.K. Construction Output (YoY) (Jan)', '9.9%', '9.2%', '7.4%', '02:00', ' GBP', '', 'GDP (MoM)', '0.8%', '0.2%', '-0.2%', '02:00', ' GBP', '', 'GDP (YoY)', '10.0%', '9.3%', '6.0%', '02:00', ' GBP', '', 'Index of Services', '1.0%', ' ', '1.2%'] 
Sign up to request clarification or add additional context in comments.

1 Comment

Incredible, I didn't know list-comprehension at all, but thank you so much for your answer, it definitely solved mi issue. I would also check that topic out, seems interesting 👍.
0

It's pretty simple:

del the_list[::8] # or del the_list[7::8] if you try to delete the 7th element of each line 

6 Comments

That would not achieve what the OP asked.
@alexpdev actually, del the_list[7::8] would work, but that's in a comment, but would be a decent answer to this question
@juanpa.arrivillaga Should the slice start at 2 instead of 7?
@Code-Apprentice no, because you want the item at index 7 to be the first item deleted, then every 8 items after that
To be precise, it's the difference between "delete every 8th element starting with the first element" versus "delete every 8th element starting with the 8th element"
|
-1

just use this code to find the element that you want to delete in that case is empty string, in python is a false string so

for x in the_list: if not x: the_list.remove(x) 

4 Comments

That would also remove those empty/false strings that are next to ` GBP` strings, which is not desired
just check the code, it would not remove that because this is part of the string
And in general, this is a bug-prone approach that is highly inefficient.
Trying to remove items while iterating this way will not give what you expect, see stackoverflow.com/questions/1207406/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.