Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Is there a specific reason why you used neighbors.extend() for a deque rather th...

Richard Yuan

Aug 5, 2022

Is there a specific reason why you used neighbors.extend() for a deque rather than neighbors.append()? The iterable only contains a single tuple in this case.

0

0

Comments
Comments
Design Gurus
Design Gurus3 years ago

You are right, Append() is more appropriate when adding one element.

Unless, we want to combine all these 'appends' into one single statement. For better readability, we put them in multiple lines.

BTW, which one looks easier to read?

-> neighbors.extend([(row + 1,...

R
Richard Yuan3 years ago

I see your point about how the bracket can make identifying missing closing brackets/parentheses easier to identify. Personally, I think neighbors.append((row + 1, col)) will be easier to read since we have 1 less pair of [] to type out with append() compared to extend...

R
Richard Yuan3 years ago

I think it makes sense for consistency that if someone initializes their deque with the cell passed in to use extend() since the format is similar:

neighbors = deque([(x, y)]) ... neighbors.extend([(row + 1, col)])

In contrast to:

neighbors = deque() neighbors.append...

On this page

Problem Statement

Try it yourself