Osama Yawar
Osama Yawar

Reputation: 369

AttributeError: 'str' object has no attribute 'pop'

I am trying to use pop with list but it is giving me the following error

AttributeError: 'str' object has no attribute 'pop'

and the way i am using pop is:

vertex = my_queue.pop()

Whats confusing is that when i use print(vertex), it print outs the data which means that it is working perfectly but showing error at the same time is confusing.`

My Code:

 def bfs(my_data):
      my_queue = [] #array to store vertices
      #print(my_data[0])
      my_queue.insert(0, my_data[0]); 
      my_data[0]['visited'] = '1';
      int_vertex = []

      while my_queue:
                vertex = my_queue.pop()
                for n_vertex in vertex['neighbors']:
                          #print(type(n_vertex))
                          int_vertex = int(n_vertex)
                          if my_data[int_vertex]['visited'] == '0':
                                    my_data[int_vertex]['visited'] = '1'
                                    test.insert(0, my_data[int_vertex])
                                    my_queue = str(test)

Where my_queue & my_data are the list and vertex is dict

Upvotes: 0

Views: 23860

Answers (4)

Ian Song
Ian Song

Reputation: 1

Because pop is not a string function you have to make your own function

Upvotes: 0

Georgi
Georgi

Reputation: 71

You could use ipdb to check at which line your list type becomes a string. When you get to the setrace, type for example the following to check the type:

type(my_queue)

Upvotes: 0

abarnert
abarnert

Reputation: 365915

Look at these two lines:

vertex = my_queue.pop()
# ...
my_queue = str(test)

So, after the first time through the loop, my_queue is obviously a str, because you explicitly convert it to one.

You can see a list of String Methods and Common Sequence Operations. Notice that pop doesn't appear anywhere there. That's because pop is only a method of Mutable Sequence Types like list. Which is exactly what this error is telling you:

AttributeError: 'str' object has no attribute 'pop'

And even if there were such a method, what would you expect it to return? The first character of the string? How would that work with for n_vertex in vertex['neighbors'], which clearly expects vertex to be a dict (or other Mapping type), not a single-character string?


I'm not sure what exactly you're trying to do at the end, but I suspect that removing the str() call won't be enough on its own to solve all of your problems. You're appending to a list named test that isn't defined anywhere in the function, so presumably it's a global variable. You then throw away whatever was in my_queue and instead assign it to that global variable. That can't possibly be what you want. My guess—but this is only a guess—is that you want to replace those last two lines with this:

my_queue.insert(0, my_data[int_vertex])

Upvotes: 5

Pavan Gupta
Pavan Gupta

Reputation: 19243

pop method is not supported by string objects in python. I believe my_queue is not referring to list object but it referring to string. via debug or using type(), isinstance() builtin you can come to know which data type my_queue is referring to.

Upvotes: 0

Related Questions