What is Python Enumerate? - PowerPoint PPT Presentation

About This Presentation
Title:

What is Python Enumerate?

Description:

Learntek is global online training provider on Big Data Analytics, Hadoop, Machine Learning, Deep Learning, IOT, AI, Cloud Technology, DEVOPS, Digital Marketing and other IT and Management courses. – PowerPoint PPT presentation

Number of Views:74
Slides: 10
Provided by: learntek12
Tags: python

less

Transcript and Presenter's Notes

Title: What is Python Enumerate?


1

PYTHON ENUMERATE
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, start0)
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
RyanenumerateActionMovies enumerate(action_mo
vies)print(type(enumerateActionMovies))
convert to listprint(list(enumerateActionMovies))
provide second parameter other than default
valueenumerateActionMovies enumerate(action_mov
ies, 10)print(list(enumerateActionMovies))  
Output ltclass enumerategt(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
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, Melbournefor c, value in
enumerate(my_travel_list, 1)print(c,
value)   Output 1 London 2 New York 3
Singapore 4 Melbourne
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,
Melbournefor c, value in enumerate(my_travel_l
ist)print(c, value)   Output 0 London 1
New York 2 Singapore 3 Melbourne
6
Example 4  In this example we create a list of
tuples containing indexes. my_hobby_list
travelling, writing, cooking,
photographycounter_list list(enumerate(my_ho
bbby_list, 1))print(counter_list)  Output (1,
travelling), (2, writing), (3, cooking),
(4, photography)  
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)prin
t(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_mo
vies)
8
Output (0, The world is not enough)(1,
Speed)(2, Saving Private Ryan)     0 The
world is not enough1 Speed2 Saving Private
Ryan     100 The world is not enough101
Speed102 Saving Private Ryan
9
For more Online Training Courses, Please
contact Email info_at_learntek.org USA 1734
418 2465 India 91 40 4018 1306 91
7799713624
Write a Comment
User Comments (0)
About PowerShow.com