Skip to content

Commit 9269285

Browse files
Update iters.md
I have added a new application of the iter() function. I have shown how we can check if an object in python is iterable or not.
1 parent 011b01f commit 9269285

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

advanced/iters.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,24 @@ does the same thing as our `count()`.
443443
- [The itertools module](https://docs.python.org/3/library/itertools.html)
444444
contains many useful iterator-related things.
445445

446+
## Checking if object is iterable or not
447+
448+
There is an easy way of checking if an object in python is iterable or not. The following code will do the needful.
449+
```python
450+
>>> def check(A):
451+
... try:
452+
... st = iter(A)
453+
... print('yes')
454+
... except:
455+
... print('no')
456+
...
457+
>>> check(25)
458+
no
459+
>>> check([25,35])
460+
yes
461+
>>>
462+
```
463+
Here you can observe that the 25 is an integer, so it is not iterable, but [25,35] is a list which is iterable so it outputs no and yes respectively.
446464
***
447465

448466
If you have trouble with this tutorial please [tell me about

0 commit comments

Comments
 (0)