Skip to content

Commit 865f136

Browse files
authored
Merge pull request Akuli#12 from debdutgoswami/patch-1
Update iters.md
2 parents 011b01f + 5014853 commit 865f136

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

advanced/iters.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,25 @@ while True:
246246
print(thing)
247247
```
248248

249+
## Checking if object is iterable or not
250+
251+
There is an easy way of checking if an object in python is iterable or not. The following code will do the needful.
252+
```python
253+
>>> def check(A):
254+
... try:
255+
... st = iter(A)
256+
... print('yes')
257+
... except TypeError:
258+
... print('no')
259+
...
260+
>>> check(25)
261+
no
262+
>>> check([25,35])
263+
yes
264+
>>>
265+
```
266+
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.
267+
249268
## Generators
250269

251270
It's possible to create a custom iterator with a class that defines an
@@ -442,7 +461,6 @@ does the same thing as our `count()`.
442461
generator runs it to the next yield and gives us the value it yielded.
443462
- [The itertools module](https://docs.python.org/3/library/itertools.html)
444463
contains many useful iterator-related things.
445-
446464
***
447465

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

0 commit comments

Comments
 (0)