TypeError: list object is not an iterator
Solution 1
Try using iter()
Ex:
item = next(iter(filter(lambda x: x['name'] == name, items)), None)
Solution 2
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.
Lutaaya Huzaifah Idris
I am Lutaaya Huzaifah Idris , a Core Experienced Mobile developer , I graduated at Islamic University in Uganda while offering Bachelors of Science in Computer Science. I love to keep the code in the air. If you need any mobile software just send me a hello and we communicate.
Updated on June 08, 2020Comments
-
Lutaaya Huzaifah Idris almost 4 years
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)
-
user2357112 almost 6 yearsYou seem to be running Python 3 code on Python 2.
-
-
Jean-François Fabre almost 6 yearsyou could add that using
iter
on an iterator also works so the code is also compatible with python 3. -
eribeiro about 5 yearsYou can also use a list comprehension to save a filter call: item = next(iter([i for i in items if i['name'] == name], None))