alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Python - Python Advanced - Iterators Tutorial

 An iterator in python is an object that is used to iterate over iterable objects like lists, tuples, dictionaries, and sets.

The iterator object which implements the iterator protocol is built using the iter() and next() methods.

The iter() function (which in turn calls the __iter__() method) returns an iterator from the iterable objects like lists, tuples, dictionaries, and sets.

The next() function (which in turn calls the __next__() method) is used to manually iterate through all the items of an iterator

 

Example-

nametuple = ("sam", "sarang", "dinesh", "ashwin")
myit = iter(nametuple)

print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))

Output-

sam
sarang
dinesh
ashwin
 

A string is also iterable

Example-

str = "sam"
myit = iter(str)

print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))

Output-

s
a
m
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
Input In [9], in <cell line: 7>()
      5 print(next(myit))
      6 print(next(myit))
----> 7 print(next(myit))
StopIteration: 

 

It will raise an error if no item is left in the iterator.

__iter__() will work same as iter() and __next__() will work same as next()

Example-

list_data  = [10,8,3,5]
my_iter = list_data.__iter__()

print(next(my_iter))
print(my_iter.__next__())
print(next(my_iter))
print(my_iter.__next__())

Output –

10
8
3
5

 

Python

Python

  • Introduction
  • Installation and running a first python program
    • How to install Python ?
    • Running 1st Hello World Python Program
    • How to install Pycharm ?
  • Things to know before proceed
    • Escape Characters/Special Characters
    • Syntax ( Indentation, Comments )
    • Variable
    • Datatype
    • Keyword
    • Literals
    • Operator
    • Precedence & Associativity Of Operator
    • Identifiers
    • Ascii, Binary, Octal, Hexadecimal
    • TypeCasting
    • Input, Output Function and Formatting
  • Decision control and looping statement
    • if-else
    • for loop
    • While loop
    • Break Statement
    • Continue Statement
    • Pass Statement
  • Datatype
    • Number
    • List
    • Tuple
    • Dictionary
    • String
    • Set
    • None & Boolean
  • String Method
    • capitalize()
    • upper()
    • lower()
    • swapcase()
    • casefold()
    • count()
    • center()
    • endswith()
    • split()
    • rsplit()
    • title()
    • strip()
    • lstrip()
    • rstrip()
    • find()
    • index()
    • format()
    • encode()
    • expandtabs()
    • format_map()
    • join()
    • ljust()
    • rjust()
    • maketrans()
    • partition()
    • rpartition()
    • translate()
    • splitlines()
    • zfill()
    • isalpha()
    • isalnum()
    • isdecimal()
    • isdigit()
    • isidentifier()
    • islower()
    • isupper()
    • isnumeric()
    • isprintable()
    • isspace()
    • istitle()
  • Python Function
    • Introduction
  • Lambda Function
    • Lambda Function
  • Python Advanced
    • Iterators
    • Module
    • File Handling
    • Exceptional Handling
    • RegEx - Regular Expression
    • Numpy
    • Pandas

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.

Toggle