Home > Software engineering >  Flask: cannot unpack non-iterable int object in Python list
Flask: cannot unpack non-iterable int object in Python list

Time:10-24

I have a list like so:

my_list = []
check_values = [1, 2, 5]

str_input = "5"

my_list.append(int(str_input))

Then I try to create a set to test some stuff:

testing = list(set(v for k, v in my_list if int(k) in check_values))

I get the following error on the line above:

Flask: cannot unpack non-iterable int object

Am I missing something?

CodePudding user response:

The "unpacking" is happening in your generator expression when Python tries to "unpack" each value of my_list into the two variables k and v.

It looks like maybe you adapted this from code that was iterating over a dict.items() instead of a plain list of integers.

Fix with:

testing = list(set(v for v in my_list if int(v) in check_values))

Or use a set union:

testing = list(set(my_list) | check_values)

CodePudding user response:

Not sure of the usage of the k variable in your for structure - what is it trying to achieve?

However, using k, v in a for is usually applicable to dictionaries, not lists. (unless I'm missing something)

Just update your code to

testing = list(set(v for v in my_list if int(v) in check_values))
  • Related