Reputation: 11
I am having a list with int values, iterating the same list with a loop, while iterating it I want to check the list, with some if condition, but am getting this issue:
TypeError: argument of type 'int' is not iterable
My code:
list_b=[1,2,3,4,5,6,7,8,9] #list with int values
for m in list_b: #storing the list in m
print(m) #printing the m
for m in list_b: # again storing the same list in m
if(10 in m): #checking for presance of 10 in the list
print('yes 10 is presant in listb')
else:
print('10 is not presant in list_b')
Upvotes: 0
Views: 6220
Reputation: 3591
You seem to have fundamentally misunderstood what for
does. You seem to think that for m in list_b
means "store list_b
in m
". But what it actually means is "go through each element of list_b
, and for each of them, temporarily store that value in m
, and then execute the code in the for-loop". So whatever you write in the for-loop gets executes once for every element of list_b
. So if you just want to print list_b
and check whether 10
is in list_b
, you should get rid of the for-loops:
list_b=[1,2,3,4,5,6,7,8,9] # list with int values
print(list_b) # printing the m
if (10 in list_b): # checking for presance of 10 in the list
print('' yes 10 is presant in list_b')
else:
print('10 is not presant in list_b')
PS "presence" and "present" don't have "a", they have "e".
Upvotes: 0
Reputation: 1028
So in
is a keyword meant to iterate across an iterable data type, such as a list or string, and check if your variable is within that iterable. So you can use it like so:
ch = "cheese"
ref = "cheese, milk, eggs"
if ch in ref:
print(True)
#Prints True
or
ch = 1
l = [1,2,3,4,5]
if ch in l:
print(True)
#Prints True
But you can't iterate across an int
, it's just one integer, so there's the source of your error
Upvotes: 2