-1

Let's say I have the following piece of code:

items = []
for index, element in elements:
     if index is 7:
         continue
     else:
         items.append(element)

I'm using selenium for web scraping in the code so I'd like to make it faster by using multiprocessing.

So it'd be this way:

items = pool.map(iterate_over_elements, elements)

def iterate_over_elements(element):
     if index is 7:
        return None
     else:
        return element

How can I send the index as a parameter in each call to iterate_over_elements()?

Ian Spitz
  • 301
  • 8
  • 18
  • Possible duplicate of [Accessing the index in Python 'for' loops](https://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops) – jpmc26 Feb 05 '18 at 23:56

1 Answers1

0

To add the index, you'll want to use enumerate:

>>> list(enumerate(['a','b','c']))
[(0, 'a'), (1, 'b'), (2, 'c')]

pool.map(fn, enumerate(elements)) will call fn with tuples of (index, element). To accept that, you'll have to change your iterate_over_elements function:

def iterate_over_elements(index_and_element):
    index, element = index_and_element
    ...
pool.map(iterate_over_elements, enumerate(elements))

or, if you have python >= 3.3, you can use Pool.starmap and have iterate_over_elements accept two arguments:

def iterate_over_elements(index, element):
    ...
pool.starmap(iterate_over_elements, enumerate(elements))
Nathan Vērzemnieks
  • 5,495
  • 1
  • 11
  • 23