Cornflour
Cornflour

Reputation: 55

Extract certain tuples from a list

I'm not sure if this is the correct way to go about this, but I have created a list of Tuples which consist of a form type and a corresponding URL. I would like to isolate those Tuples which have a form type of 10-K and have tried using a code which I found on Stack Overflow. However I have ended up with a new list with no contents, despite the fact I know that there are several 10-K form types. I am a beginner so please be kind.

file = '10-K'
req_urls = []

for tuple in typeandurls:
     if file in tuple:
           req_urls.append
     else:
           pass

Upvotes: 0

Views: 44

Answers (1)

Aleks J
Aleks J

Reputation: 283

Alternatively a good way could be to use a list comprehension, like so:

file = '10-K'
req_urls = [tup for tup in typeandurls if file in tup]

Here I assume that file in tuple is how you want to test if an element should belong to the output collection.

Upside of doing that is that your code would be more readable at first sight

Upvotes: 1

Related Questions