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.
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
Subreddit
Post Details
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnpython...