Here's my function:
def printSubnetCountList(countList): print type(countList) for k, v in countList: if value: print "Subnet %d: %d" % key, value
Here's the output when the function is called with the dictionary passed to it:
<type 'dict'> Traceback (most recent call last): File "compareScans.py", line 81, in <module> printSubnetCountList(subnetCountOld) File "compareScans.py", line 70, in printSubnetCountList for k, v in countList: TypeError: 'int' object is not iterable
Any ideas?
How to Fix Int Object is Not Iterable. One way to fix it is to pass the variable into the range() function. In Python, the range function checks the variable passed into it and returns a series of numbers starting from 0 and stopping right before the specified number.
Dictionaries are themselves not an iterator (which can only be iterated over once). You usually make them an iterable, an object for which you can produce multiple iterators instead.
To iterate through a dictionary in Python, there are four main approaches you can use: create a for loop, use items() to iterate through a dictionary's key-value pairs, use keys() to iterate through a dictionary's keys, or use values() to iterate through a dictionary's values.
Try this
for k in countList: v = countList[k]
Or this
for k, v in countList.items():
Read this, please: Mapping Types — dict
— Python documentation
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