Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5014853

Browse filesBrowse files
Update iters.md
1 parent 9269285 commit 5014853
Copy full SHA for 5014853

File tree

Expand file treeCollapse file tree

1 file changed

+19
-19
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+19
-19
lines changed
Open diff view settings
Collapse file

‎advanced/iters.md‎

Copy file name to clipboardExpand all lines: advanced/iters.md
+19-19Lines changed: 19 additions & 19 deletions
  • Display the source diff
  • Display the rich diff
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,25 +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-
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.
464464
***
465465

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.