FAQ: Graph Search: Python - Breadth-First Search 2: The Breadthening

This community-built FAQ covers the “Breadth-First Search 2: The Breadthening” exercise from the lesson “Graph Search: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Breadth-First Search 2: The Breadthening

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello, please can someone explain what this line of code is doing?

current_vertex, path = bfs_queue.pop(0)

I’m getting confused because bfs_queue is a list that contains a list (and potentially multiple lists). I don’t understand how this line of code is able to access the items in the list that’s inside the bfs_queue.

We would need to have greater wherewithal as to just what ‘bfs_queue’ pertains to, in terms of structure. From the assignment we can only ascertain that each element has two members which we unpack from the popped item and give to respective variables.

Oops, didn’t read that far; my bad. Okay, so the above assumption goes along with that. Is the structure,

[
  [a, b],
  #[.,.],
  [x, y]
]

?

Yes, something like that. Here’s a snippet of the actual code for greater context:

def bfs(graph, start_vertex, target_value):
  path = [start_vertex]
  vertex_and_path = [start_vertex, path]
  bfs_queue = [vertex_and_path]
  visited = set()
  # Continue the function here:
  while bfs_queue:
    current_vertex, path = bfs_queue.pop(0)

I don’t understand how the last line of code is actually able to access the items in the list inside bfs_queue. Shouldn’t that line of code be something like:

current_vertex, path = bfs_queue.pop(0)[0]

to be able to do that?

1 Like

There would not be enough items to unpack.

Let’s say we have popped off the two element list item from the start of the queue and it looks something like this:

[21, 42]

We know there are two items in the list so to unpack it we assign to two variables:

a, b = [21, 42]

print (a, b)   #    21 42

Oh, so does python allow you to access elements in a list without having to pop them out of the list using this syntax, then? I think that’s what got me confused. If that’s the case, then it makes a lot of sense now. Thank you!

1 Like

We can unpack any iterable sequence:

y = {2, 3, 5, 7}    #  set
>>> a, b, c, d = y
>>> a
2
>>> b
3
>>> c
5
>>> d
7
>>>
>>> y = (11, 13, 17, 19)  #  tuple
>>> m, n, o, p = y
>>> m
11
>>> n
13
>>> o
17
>>> p
19
>>>
>>> y = [23, 29, 31, 37]  #  list
>>> q, r, s, t = y
>>> q
23
>>> r
29
>>> s
31
>>> t
37
>>>
>>> y = 41, 47, 53, 57  #  literal sequence
>>> u, v, w, x = y
>>> u
41
>>> v
47
>>> w
53
>>> x
57
>>> 
1 Like

I see. Didn’t even know there’s a data structure that’s literally a sequence of literals. Thank you so much for explaining this to me. It makes so much sense now.

1 Like

For this language, anyway… Internally Python dresses it up as a tuple, or immutable sequence. It is indicative of an unknown number of sequence members and the basis of *args in function parameters, which I’m sure you will enjoy reading, and learning from, now that this idea has been revealed.

Be mindful to not take liberties as much as make prudent and apt use of this built in behavioral feature. Utilize the part that makes sense; otherwise make it clear the data structure you want your reader to recognize.

Always excited to see a learner tackling the curve. Happy coding!

1 Like

Yes, I’ve come across *args before. Thanks!

1 Like
>>> def foo (*args):
...     return [args]
... 
>>> foo (1,2,3,4,5)
[(1, 2, 3, 4, 5)]
>>> def foo (*args):
...     return [*args]
... 
>>> foo (1,2,3,4,5)
[1, 2, 3, 4, 5]
>>> 

Draw from this whatever meaningful information you can carry forward with you. And really go to town messing with this stuff while you have the mind to, before you move on from it.

This is interesting. I’m not quite sure that I fully understand, but it looks like the keeping * allows the sequence maintain it’s status as simply a sequence of literals, otherwise it defaults to a tuple? I’ll definitely test it out on a code editor to see if that’s the case. Thank you!

1 Like

The star is what we used to call the splat operator. It may still be called that, for all I know. It is part of the suite of unpacking tools, so definitely something to become fully aware of.

Oh, yeah, the default is always a tuple since that is the most efficient data structure that Python has at its disposal (we have to create a more efficient one, if we want it for our purposes).

1 Like

I tested out these lines of code and got these output:


args = 1, 2, 3
print(args)
# Output: (1, 2, 3)
print([*args])
#Output: [1, 2, 3]
print(*args)
#Output: 1 2 3

I’m glad I understand how this works a little more than I did before, and I’ll definitely read up about it to see where it might be useful. Thank you so much!

1 Like
>>> def foo (*args):
...     return {*args}
... 
>>> foo (1,2,3,4,5)
{1, 2, 3, 4, 5}
>>> 

Just to round it out. Remember to never treat sets as ordered objects. And they have their own methods outside of the ordinary ones.

1 Like

Sets just look like dictionaries with values that don’t have keys haha. I’ve never had to use them before asides this lesson, but I’ll definitely remember that.

1 Like

They only look like dictionaries because there is no other suitable bracket character for their type. I guess we could call them, ‘keyless dictionaries’, although that would be a misnomer. They are a unique form of data structure so we should not confuse them, or associate them in any way with dictionaries. If you don’t see the colon, it’s a set. Completely different set of rules apply.

So we have so far covered how many types of unique data structures?

Oh that makes sense. I gave a sets a quick read and see that they indeed have methods that are quite unique to them. They have methods that resemble the operations we can perform on sets in mathematics.

I’m thinking 5, if we include dictionaries.

Precisely. Once we learn the syntax and usage, we can do with Python what we can do with pencil on paper.

Unions, intersections and disparity are mathematical principles that the language gives us tools for ready use. That is another lesson/course just waiting for you. Get past the language hurdles, first, though, so definitely back burner this one.

There is a relation between sets and dictionaries, but more ethereal than programmatically significant. Sets have no duplicate members. Dictionaries, if we confine the keys to an object we will also discover to be a set. No duplicate keys. So it’s actually okay to call the contents of a set or a dictionary, ‘members’.

We can actually leverage set theory in working with dictionaries along the lines mentioned above. Don’t ask me how that would play out, but leave it on the back burner as something that may come in handy, some day, since you are on the subject of Search algorithms. Could be these ideas might go a long way to optimizing your code. Who knows?

I will keep these in mind. I have definitely enjoyed applying what I know from maths to programming; it also makes it easier to understand programming concepts. I think it would also be interesting to see what kinds of problems will require me to use dictionaries in the way that I would normally use sets. Maybe I can write my own set functions but for dictionaries.

1 Like