Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Nataly Rifold
Nataly Rifold
12,431 Points

In the Dungeon Game video, why use remove and not pop?

So, why remove and not pop? I mean remove deletes a value (as I understand). So how come is doesn't delete permanently the removed value?

And why x, y = player and not player = x, y?

1 Answer

remove and pop work differently. :point_left:

  • remove searches for the argument provided within the list, and if it finds it, it will remove it. It returns nothing, and it only deletes the first find.
  • pop takes an index as a value, and "pops" the element at that index. It returns the element it popped. It doesn't search for the argument, as it directly takes in an index.

So I suppose in this case it is easier to use remove instead of pop.

As for player = x, y and x, y = player, they are two completely different statements. (No, they don't do the same thing.) The first statement assigns player to a tuple containing the values (x, y). The second statement "unpacks" player, and assigns each variable to the corresponding value in the tuple player. For more info on unpacking, watch this video.

I hope this clears things up! :grin: