Am trying to make a simple post api in flask-python but am getting this error :
TypeError: list object is not an iterator
but when i revise my code seems fine what could be the problem.
My function which specifically has the problem:
def post(self,name):
#return {'message': name}
item = next(filter(lambda x: x['name'] == name, items), None)
if item:
return {'message':"An item with name '{}' already exixts. ".format(name)},400
data = request.get_json()
item = {'name': name, 'price':data['price']}
items.append(item)
return item, 201
When i try to post something on postman i get this logcat
error:
[2018-06-07 10:41:02,849] ERROR in app: Exception on /item/test [POST]
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Python27\lib\site-packages\flask_restful\__init__.py", line 480, in wrapper
resp = resource(*args, **kwargs)
File "C:\Python27\lib\site-packages\flask\views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "C:\Python27\lib\site-packages\flask_restful\__init__.py", line 595, in dispatch_request
resp = meth(*args, **kwargs)
File "G:\flask_workspace\MealBookingApp\MealBookingApp\MealBookingApp\views.py", line 30, in post
item = next(filter(lambda x: x['name'] == name, items), None)
TypeError: list object is not an iterator
127.0.0.1 - - [07/Jun/2018 10:41:02] "POST /item/test HTTP/1.1" 500 -
NB:
line 30 , is the line below :
item = next(filter(lambda x: x['name'] == name, items), None)
A list object is not an iterator because it does not have a __next__() method. Thus calling the next() function (which would call the __next__() method on an object if it has one) on a list would result in a TypeError, since our list object is not an iterator and thus does not have a __next__() method.
For example, a list is iterable but a list is not an iterator. An iterator can be created from an iterable by using the function iter(). To make this possible, the class of an object needs either a method __iter__, which returns an iterator, or a __getitem__ method with sequential indexes starting with 0.
We tried to use the list as an iterator, but lists are not iterators (they are iterable). To solve the error, pass the list to the iter() function to get an iterator.
An object is called iterable if we can get an iterator from it. Most built-in containers in Python like: list, tuple, string etc. are iterables.
Try using iter()
Ex:
item = next(iter(filter(lambda x: x['name'] == name, items)), None)
To elaborate on @Rakesh's answer, lists aren't iterators, and the output of filter()
in Python 2 is a list. To fix this, you can use the iter()
function to output an iterator corresponding to the problematic list so that next()
can be called appropriately. The same code then should solve your problem:
item = next(iter(filter(lambda x: x['name'] == name, items)), None)
Note that using iter()
on an iterator still works in Python 3, so this code is forward compatible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With