Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Enumerate with a returned list query
Post Body

Hi, I'm currently going through the book Elements of Programming Interviews and I have a query about a solution that they have written.

The question is about finding the intersection of two sorted lists. The initial solution they gave is below.

return [a for i, a in enumerate(A) if a in B and (i == 0 or a != A[i - 1])]

I was confused about the second conditional statement in brackets but now get that it's checking the index of the returned array (I think, i.e. if it will be the first element it won't be a duplicate, or if it's not the same as the last element in the returned array).

What I don't understand is that I thought the variable 'i' was in relation to enumerate(A) and not the returned array and so was wondering if someone could give me a bit of insight on this? The book didn't explain the syntax. Cheers

Author
Account Strength
80%
Account Age
4 years
Verified Email
Yes
Verified Flair
No
Total Karma
213
Link Karma
59
Comment Karma
154
Profile updated: 5 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago