Interview question: what is the difference between .pop() and .drop() in Python?

Crystal X
Geek Culture
Published in
2 min readMar 6, 2023

--

I have been studying Python’s library of neural networks, Tensorflow, and have been looking at the sample code on the Tensorflow website in an attempt to learn how to use the library properly.

One thing that I noticed when I was reviewing the Tensorflow code was the fact that the developers of the library use .pop() instead of .drop() when seeking to drop a column from a dataframe. This piqued my interest, so I decided to research the subject to find out exactly why that is.

Both pop() and drop() are functions used to remove elements from a list or an array in Python, but they have slightly different behaviour:

  1. pop() method: The pop() method removes and returns the last element from a list or an array. If an index is provided as an argument, it will remove and return the element at that index.
  2. drop() method: The drop() is not a built-in method in Python, but it is used in some libraries like Pandas. The drop() function in Pandas is used to remove specified rows or columns from a DataFrame. The way to remove the last element of a list in Python is:-

In summary, pop() is a built-in method in Python used to remove and return an element from a list or array, while drop() is a function in Pandas used to remove…

--

--

Crystal X
Geek Culture

I have over five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.