What is indexing, iterating and enumerating in python?


Indexing

“Indexing” means addressing an element of a sequential object by its position within the sequential object.  A sequential object is in general, an ordered collection. In python, every element of an ordered collection is accessed based on their position in the collection. An index is a numerical form of position. Python uses square bracket notation[] to index collections. An index of elements in a collection ranges between [0 ] to [len(collection)-1], other values will give IndexError.

Dictionaries in python are an unordered collection of key : value pairs. In dictionaries, related information is associated with keys. Even though dictionaries are unordered, their values can be accessed by their keys.

Python allows negative indexing i.e. indexing from the end of the list using a negative number. Negative index of a collection ranges between [-1] to [-len(collection)].


Iterating

“Iterating” or “looping” means repeating the execution of a similar task. Python for loop statement loops over iterables to execute the same block of code again and again.  An iterable is an object which can return its element one by one. Sequences are ordered sets of iterables that have specific set of features. Lists, tuples, and strings are all sequences. Apart from sequences, collections like sets and dictionaries are also built-in iterables in python. File objects and generators are other iterables in python.

To manually iterate through the items of an iterable object iter() and next() built-in methods are used. An iterator is an actual iterable object that is iterated. The iter() function creates an iterator object. A repeated passing of iterator to the next() function returns successive items in the iterable. If no more elements are available in the iterable then a StopIteration exception is raised.

Python for loops automatically iterates through the items of an iterable object, executing the block of code each time. The for loop creates an iterator object and executes the next() method over and over until a StopIteration exception is raised which is internally caught and the loop ends. Iterating through iterators using for loop does not require indexes of iterables they directly iterate through each item of iterables using in operator.:

for element in iterable:

#block of code


Enumerating

“Enumerating” basically means looping over iterators and getting both value and index of each item of the sequence. A lot of times when working with iterators, there’s a need to keep a count of the number of iterations, or many new programmers needs to know both values and indexes of each element of iterators for better understanding of programming, so enumeration can be used. Enumeration is mainly used by professionals or beginners for creating a dictionary as it returns keys as well as values.

The traditional programming way to do enumeration is by using loops.

Dictionary is a key : value pair collection. Values of the dictionary are referred by its keys, not by indexes. Thus this method can’t be used to enumerate a dictionary.

Apart from the above method, Python offers a simple built-in function enumerate(), which can not only iterate over the index of an item, as well as the item itself but also makes a program look cleaner. The enumerate() function assigns an index to each item in an iterable object and returns it in the form of an enumerate object. The enumerate method is an iterator that produces a tuple of the form (index, element) for each element. This enumerate object can then be used directly in for loops or be converted into a list of tuples using list() method. The syntax of enumerate() is:

enumerate(iterable, start=0)

In enumerate(), counting starts from start and by default, its value is 0.

Each tuple produced by the enumerated object can be unpacked to two different variables representing index and element.

 

Tags: enumerating, enumerating in python, indexing, indexing different data types, indexing in python, iterating, iterating and enumerating in python?, iterating in python, iterating through different data types, negative indexing in python, uses of enumeration, What is indexing, enumerate(), enumerate() in python, enumerating different data types, enumerating different iterators, enumerate different iterators

Add Comment

%d bloggers like this: