0

I am in a situation where I have 3 nested loops. Every x iterations, I want to restart the 2nd for loop. If an element in the 3rd for loop meets a certain condition, I want to remove that element from the list.

I'm not sure how to implement this and using a list comprehension or creating a new list wouldn't really work based on the similar questions I read.

Example pseudocode:

items_of_interest = ["apple", "pear"]

while True: # restart 10,000 iterations (API key only last 10,000 requests)
    api_key = generate_new_api_key()
    for i in range(10000):
        html = requests.get(f"http://example.com/{api_key}/items").text
        for item in items_of_interest:
            if item in html:
                items_of_interest.remove(item)

The original code is a lot bigger with a lot of checks, constantly parsing an API for something, and it's a bit messy to organize as you can tell. I'm not sure how to reduce the complexity.

bird
  • 13
  • 1

2 Answers2

1

Without knowing the full picture, it's hard to say which approach is optimal. In any case, here's one approach using comprehension.

items_of_interest = ["apple", "pear"]

while True: # restart 10,000 iterations (API key only last 10,000 requests)
    api_key = generate_new_api_key()
    for i in range(10000):
        html = requests.get(f"http://example.com/{api_key}/items").text

        # Split your text blob into separate strings in a set
        haystack = set(html.split(' '))
        # Exclude the found items!
        items_of_interest = list(set(items_of_interest).difference(haystack))
kerwei
  • 1,822
  • 1
  • 13
  • 22
  • About to write a similar answer, with a twist: what about using `difference` instead of `intersection` and avoiding the list comprehension at all? – Francesco Apr 17 '19 at 06:37
  • @Francesco It's out of habit! I use `intersection` almost all the time. I'll update my answer to use `difference` if you don't mind? – kerwei Apr 17 '19 at 06:43
  • please do. I like your effort to simplify the code with less nesting and a more declarative style – Francesco Apr 17 '19 at 06:44
  • As a suggestion for the OP, if you choose this implementation, you should consider defining `items_of_interest` as a set in the first place – Francesco Apr 17 '19 at 06:53
  • @Francesco I retained that because it's defined that way in the OP's question :) – kerwei Apr 17 '19 at 06:54
0

It works much like you suggest. The relevant keyword is del. eg

>>> x = range(5)
>>> for i in ['a','b','c']:
...     print ('i:' + str(i) )
...     for j in x:
...         print('j:' + str(j))
...     if j == 3:
...             del x[j]
...
i:a
j:0
j:1
j:2
j:3
i:b
j:0
j:1
j:2
j:4
i:c
j:0
j:1
j:2
j:4

3 has been removed from the list x for the later passes.

See also Python doco https://docs.python.org/3.7/tutorial/datastructures.html and SO answers like Difference between del, remove and pop on lists

Adam Burke
  • 724
  • 1
  • 7
  • 21