3.6 Some built-in functions

Now let's check out some other important built-in functions that will come in handy such as range(), enumerate(), zip(), sorted(), filter() and map().

1. range(start_index=0, end_index, step=1) => range

Parameters:

  • >start_index Optional[int]: The start index for iteration, default is 0.
  • >end_index int: The stopping index for iteration, it is a required argument.
  • >step Optional[int]: Number of indexes to skip on iteration, default is 1.

Explanation: This function returns a sequence of length starting from start_index to end_index. The range() function returns a range object, which is iterable, supports indexing and is immutable. It is mainly used in loops, where a certain number of times a loop should work, like for iterating to the length of an array in C/C++/Java.

2. enumerate(iterable) => enumerate

Parameters:

  • >iterable iterable: Iterable object containing items, it is a required argument.

Explanation: This function returns a enumerate object given an iterable, each item is a tuple which contains index & value. The index is in range from 0 to length of the iterable provided and value is an item from that iterable. The enumerate object is iterable and indexing/slicing is not supported. Similar to range, enumerate is mostly used in iteration of loops, but here we use a predefined iterable while creating the object.

3. zip(*iterable) => zip

Parameters:

  • >iterable iterable: Iterable object containing items, it is a required argument. Note the '*' denotes a function that can take multiple input objects.

Explanation: This function returns a zip object given single/multiple iterables, each item in a zip is a tuple which contains n (number of input iterables) length of elements. When provided with multiple iterables, the length of the returned zip object is equal to the length of the smallest iterable. zip() is commonly used to unpack values from multiple iterables simultaneously in a loop.

4. sorted(iterable, key=None, reverse=False) => list

Parameters:

  • >iterable iterable: Iterable object containing items, it is a required argument.
  • >key Optional[function]: Optional function to fetch values from the iterable object, default is None.
  • >reverse Optional[bool]: Optional to reverse the sorting, default is False.

Explanation: This function returns a sorted list given an iterable object. Sorting is O(nLogn). key parameter takes a function which is then used to extract the elements, helpful when an object has some inner structure. sorted() function has a reverse parameter, which is used to do reverse sorting.

5. filter(function, iterable) => filter

Parameters:

  • >function function: A function for filtering, it is a required argument.
  • >iterable iterable: Iterable object containing items, it is a required argument.

Explanation: This function takes an input function & an iterable and applies that function on every item of that iterable. filter() returns only if True condition is met, if False is met nothing is returned, also if no condition is met nothing is returned. filter() as the name suggests, is used to filter out non-required values from an iterable object. filter() returns a filter object which is iterable and indexing/slicing is not supported.

6. map(function, iterable) => map

Parameters:

  • >function function: A function to apply on items, it is a required argument.
  • >iterable iterable: Iterable object containing items, it is a required argument.

Explanation: This function takes an input function & an iterable object and applies that function on every item of that iterable. As the name suggests, a function is mapped to each element of an iterable. So unlike filter(), map() returns the direct value returned by our input function.