SlideShare a Scribd company logo
1 of 9
PYTHON ENUMERATE
Copyright @ 2018 Learntek. All Rights Reserved. 2
Python Enumerate :
python enumerate() is basically a python built in function. During certain situations where we deal with lot of
iterators we also need to keep count of iterations, in these type of situations python built in function Enumerate
comes handy. It helps to add counter to the iterable and returns an enumerate object.
The basic syntax :-
The enumerate() function takes two parameters namely :
• iterable – It is a sequence, an iterator or objects which support iteration.
• start – It is an optional parameter and enumerate starts to count if provided. By default, 0 is set if start
parameter is not provided.
The returned enumerate object can be converted into list and tuple using list() and tuple() method.
Let us see some examples of enumerate() function.
enumerate(iterable, start=0)
Copyright @ 2018 Learntek. All Rights Reserved.
3
Example 1: The below example creates an enumerate object using enumerate() function. We can check the type
of the object by using another built in function type(). We also convert the enumerate object to list by using list()
function.
action_movies = [‘The world is not enough’, ‘Speed’, ‘Saving Private Ryan’]
enumerateActionMovies = enumerate(action_movies)
print(type(enumerateActionMovies))
# convert to list
print(list(enumerateActionMovies))
# provide second parameter other than default value
enumerateActionMovies = enumerate(action_movies, 10)
print(list(enumerateActionMovies))
Output:
<class ‘enumerate’>
[(0, ‘The world is not enough’), (1, ‘Speed’), (2, ‘Saving Private Ryan’)]
[(10, ‘The world is not enough’), (11, ‘Speed’), (12, ‘Saving Private Ryan’)]
Copyright @ 2018 Learntek. All Rights Reserved. 4
Example 2:
Passing second parameter.
In the below example we will print out the list of travel destination along with the count. Do note here second
parameter is starting with 1.
my_travel_list = [‘London’, ‘New York’, ‘Singapore’, ‘Melbourne’]
for c, value in enumerate(my_travel_list, 1):
print(c, value)
Output:
# 1 London
# 2 New York
# 3 Singapore
# 4 Melbourne
Copyright @ 2018 Learntek. All Rights Reserved. 5
Example 3 :
Without second parameter. Here we just omitted the second parameter and enumerate started count from 0.
my_travel_list = [‘London’, ‘New York’, ‘Singapore’, ‘Melbourne’]
for c, value in enumerate(my_travel_list):
print(c, value)
Output:
# 0 London
# 1 New York
# 2 Singapore
# 3 Melbourne
Copyright @ 2015 Learntek. All Rights Reserved. 6
Example 4:
In this example we create a list of tuples containing indexes.
my_hobby_list = [‘travelling’, ‘writing’, ‘cooking’, ‘photography’]
counter_list = list(enumerate(my_hobbby_list, 1))
print(counter_list)
Output:
[(1, ‘travelling’), (2, ‘writing’), (3, ‘cooking’), (4, ‘photography’)]
Copyright @ 2015 Learntek. All Rights Reserved. 7
Example 5 :
In this example we will iterate over the enumerate object.
action_movies = [‘The world is not enough’, ‘Speed’, ‘Saving Private Ryan’]
for movie in enumerate(action_movies):
print(movie)
print(‘n’)
for count, movie in enumerate(action_movies):
print(count, movie)
print(‘n’)
# Provide second parameter other than default value
for count, movie in enumerate(action_movies, 100):
print(count, movie)
enumerateActionMovies = enumerate(action_movies)
Copyright @ 2015 Learntek. All Rights Reserved. 8
Output:
(0, ‘The world is not enough’)
(1, ‘Speed’)
(2, ‘Saving Private Ryan’)
0 The world is not enough
1 Speed
2 Saving Private Ryan
100 The world is not enough
101 Speed
102 Saving Private Ryan
Copyright @ 2018 Learntek. All Rights Reserved. 9
For more Online Training Courses, Please contact
Email : info@learntek.org
USA : +1734 418 2465
India : +91 40 4018 1306
+91 7799713624

More Related Content

What's hot

Python Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.inPython Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.in
Learnbayin
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
David Livingston J
 

What's hot (20)

operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
 
Python : basic operators
Python : basic operatorsPython : basic operators
Python : basic operators
 
Python Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.inPython Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.in
 
Cse lecture-4.1-c operators and expression
Cse lecture-4.1-c operators and expressionCse lecture-4.1-c operators and expression
Cse lecture-4.1-c operators and expression
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
2 arrays
2   arrays2   arrays
2 arrays
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
In what way can C++0x standard help you eliminate 64-bit errors
In what way can C++0x standard help you eliminate 64-bit  errorsIn what way can C++0x standard help you eliminate 64-bit  errors
In what way can C++0x standard help you eliminate 64-bit errors
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
The Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterateThe Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterate
 
Python Operators
Python OperatorsPython Operators
Python Operators
 
Python operators
Python operatorsPython operators
Python operators
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 
Python Basic Operators
Python Basic OperatorsPython Basic Operators
Python Basic Operators
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
 
3. user input and some basic problem
3. user input and some basic problem3. user input and some basic problem
3. user input and some basic problem
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
 
Consider the following C program: int fun(int *i) { *i += 5; return 4; } void...
Consider the following C program: int fun(int *i) { *i += 5; return 4; } void...Consider the following C program: int fun(int *i) { *i += 5; return 4; } void...
Consider the following C program: int fun(int *i) { *i += 5; return 4; } void...
 
oop Lecture 6
oop Lecture 6oop Lecture 6
oop Lecture 6
 

Similar to Python enumerate

9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
irdginfo
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
rohithprabhas1
 
Python advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmPython advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithm
John(Qiang) Zhang
 

Similar to Python enumerate (20)

Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
C Programming - Refresher - Part III
C Programming - Refresher - Part IIIC Programming - Refresher - Part III
C Programming - Refresher - Part III
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
unit 1.docx
unit 1.docxunit 1.docx
unit 1.docx
 
PE1 Module 2.ppt
PE1 Module 2.pptPE1 Module 2.ppt
PE1 Module 2.ppt
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)
 
Python - Data Collection
Python - Data CollectionPython - Data Collection
Python - Data Collection
 
Python Built-in Functions and Use cases
Python Built-in Functions and Use casesPython Built-in Functions and Use cases
Python Built-in Functions and Use cases
 
Python-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdfPython-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdf
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
 
Python
PythonPython
Python
 
Python PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional ExecutionPython PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional Execution
 
Handout # 6 pointers and recursion in c++
Handout # 6   pointers and recursion in c++Handout # 6   pointers and recursion in c++
Handout # 6 pointers and recursion in c++
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
 
Python PPT2
Python PPT2Python PPT2
Python PPT2
 
Pointers
PointersPointers
Pointers
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
 
Python advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmPython advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithm
 
ACM init()- Day 4
ACM init()- Day 4ACM init()- Day 4
ACM init()- Day 4
 

More from Janu Jahnavi

More from Janu Jahnavi (20)

Analytics using r programming
Analytics using r programmingAnalytics using r programming
Analytics using r programming
 
Software testing
Software testingSoftware testing
Software testing
 
Software testing
Software testingSoftware testing
Software testing
 
Spring
SpringSpring
Spring
 
Stack skills
Stack skillsStack skills
Stack skills
 
Ui devopler
Ui devoplerUi devopler
Ui devopler
 
Apache flink
Apache flinkApache flink
Apache flink
 
Apache flink
Apache flinkApache flink
Apache flink
 
Angular js
Angular jsAngular js
Angular js
 
Mysql python
Mysql pythonMysql python
Mysql python
 
Mysql python
Mysql pythonMysql python
Mysql python
 
Ruby with cucmber
Ruby with cucmberRuby with cucmber
Ruby with cucmber
 
Apache kafka
Apache kafkaApache kafka
Apache kafka
 
Apache kafka
Apache kafkaApache kafka
Apache kafka
 
Google cloud platform
Google cloud platformGoogle cloud platform
Google cloud platform
 
Google cloud Platform
Google cloud PlatformGoogle cloud Platform
Google cloud Platform
 
Apache spark with java 8
Apache spark with java 8Apache spark with java 8
Apache spark with java 8
 
Apache spark with java 8
Apache spark with java 8Apache spark with java 8
Apache spark with java 8
 
Categorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonCategorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk python
 
Categorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk pythonCategorizing and pos tagging with nltk python
Categorizing and pos tagging with nltk python
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

Python enumerate

  • 2. Copyright @ 2018 Learntek. All Rights Reserved. 2 Python Enumerate : python enumerate() is basically a python built in function. During certain situations where we deal with lot of iterators we also need to keep count of iterations, in these type of situations python built in function Enumerate comes handy. It helps to add counter to the iterable and returns an enumerate object. The basic syntax :- The enumerate() function takes two parameters namely : • iterable – It is a sequence, an iterator or objects which support iteration. • start – It is an optional parameter and enumerate starts to count if provided. By default, 0 is set if start parameter is not provided. The returned enumerate object can be converted into list and tuple using list() and tuple() method. Let us see some examples of enumerate() function. enumerate(iterable, start=0)
  • 3. Copyright @ 2018 Learntek. All Rights Reserved. 3 Example 1: The below example creates an enumerate object using enumerate() function. We can check the type of the object by using another built in function type(). We also convert the enumerate object to list by using list() function. action_movies = [‘The world is not enough’, ‘Speed’, ‘Saving Private Ryan’] enumerateActionMovies = enumerate(action_movies) print(type(enumerateActionMovies)) # convert to list print(list(enumerateActionMovies)) # provide second parameter other than default value enumerateActionMovies = enumerate(action_movies, 10) print(list(enumerateActionMovies)) Output: <class ‘enumerate’> [(0, ‘The world is not enough’), (1, ‘Speed’), (2, ‘Saving Private Ryan’)] [(10, ‘The world is not enough’), (11, ‘Speed’), (12, ‘Saving Private Ryan’)]
  • 4. Copyright @ 2018 Learntek. All Rights Reserved. 4 Example 2: Passing second parameter. In the below example we will print out the list of travel destination along with the count. Do note here second parameter is starting with 1. my_travel_list = [‘London’, ‘New York’, ‘Singapore’, ‘Melbourne’] for c, value in enumerate(my_travel_list, 1): print(c, value) Output: # 1 London # 2 New York # 3 Singapore # 4 Melbourne
  • 5. Copyright @ 2018 Learntek. All Rights Reserved. 5 Example 3 : Without second parameter. Here we just omitted the second parameter and enumerate started count from 0. my_travel_list = [‘London’, ‘New York’, ‘Singapore’, ‘Melbourne’] for c, value in enumerate(my_travel_list): print(c, value) Output: # 0 London # 1 New York # 2 Singapore # 3 Melbourne
  • 6. Copyright @ 2015 Learntek. All Rights Reserved. 6 Example 4: In this example we create a list of tuples containing indexes. my_hobby_list = [‘travelling’, ‘writing’, ‘cooking’, ‘photography’] counter_list = list(enumerate(my_hobbby_list, 1)) print(counter_list) Output: [(1, ‘travelling’), (2, ‘writing’), (3, ‘cooking’), (4, ‘photography’)]
  • 7. Copyright @ 2015 Learntek. All Rights Reserved. 7 Example 5 : In this example we will iterate over the enumerate object. action_movies = [‘The world is not enough’, ‘Speed’, ‘Saving Private Ryan’] for movie in enumerate(action_movies): print(movie) print(‘n’) for count, movie in enumerate(action_movies): print(count, movie) print(‘n’) # Provide second parameter other than default value for count, movie in enumerate(action_movies, 100): print(count, movie) enumerateActionMovies = enumerate(action_movies)
  • 8. Copyright @ 2015 Learntek. All Rights Reserved. 8 Output: (0, ‘The world is not enough’) (1, ‘Speed’) (2, ‘Saving Private Ryan’) 0 The world is not enough 1 Speed 2 Saving Private Ryan 100 The world is not enough 101 Speed 102 Saving Private Ryan
  • 9. Copyright @ 2018 Learntek. All Rights Reserved. 9 For more Online Training Courses, Please contact Email : info@learntek.org USA : +1734 418 2465 India : +91 40 4018 1306 +91 7799713624