cdub
cdub

Reputation: 25731

Flask: cannot unpack non-iterable int object in Python list

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?

Upvotes: 0

Views: 1149

Answers (2)

pyhalex
pyhalex

Reputation: 19

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))

Upvotes: 1

soundstripe
soundstripe

Reputation: 1474

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)

Upvotes: 3

Related Questions