0% found this document useful (0 votes)
10 views2 pages

9 1-Iterators

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

9 1-Iterators

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

7/18/24, 10:44 AM 9.1-Iterators.

ipynb - Colab

keyboard_arrow_down Iterators
Iterators are advanced Python concepts that allow for efficient looping and memory management. Iterators provide a way to access elements
of a collection sequentially without exposing the underlying structure.

my_list=[1,2,3,4,5,6]
for i in my_list:
print(i)

1
2
3
4
5
6

type(my_list)

list

print(my_list)

[1, 2, 3, 4, 5, 6]

## Iterator
iterator=iter(my_list)
print(type(iterator))

<class 'list_iterator'>

iterator

<list_iterator at 0x27a325efb20>

## Iterate through all the element

next(iterator)

---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
Cell In[17], line 3
1 ## Iterate through all the element
----> 3 next(iterator)

StopIteration:

iterator=iter(my_list)

try:
print(next(iterator))
except StopIteration:
print("There are no elements in the iterator")

There are no elements in the iterator

# String iterator
my_string = "Hello"
string_iterator = iter(my_string)

print(next(string_iterator)) # Output: H
print(next(string_iterator)) # Output: e

H
e

Start coding or generate with AI.

https://colab.research.google.com/drive/1tTGpuUN6Loawxm3H3iujxs6bkL24Hduh#printMode=true 1/2
7/18/24, 10:44 AM 9.1-Iterators.ipynb - Colab

Start coding or generate with AI.

Start coding or generate with AI.

Start coding or generate with AI.

Start coding or generate with AI.

https://colab.research.google.com/drive/1tTGpuUN6Loawxm3H3iujxs6bkL24Hduh#printMode=true 2/2

You might also like