10 Python Built-in Functions Every Data Scientist Should Know | by Soner Yıldırım | Towards Data Science

Python is the number one choice of programming language for many data scientists and analysts. One of the reasons of this choice is that python is relatively easier to learn and use. More importantly, there is a wide variety of third party libraries that ease lots of tasks in the field of data science. For instance, numpy and pandas are great data analysis libraries. Scikit-learn and tensorflow are for machine learning and data preprocessing tasks. We also have python data visualization libraries such as matplotlib and seaborn. There are many more useful libraries for data science in python ecosystem.
In this post, we will not talk about these libraries or frameworks. We will rather cover important built-in functions of python. Some of these functions are also used in the libraries we mentioned. There is no point in reinventing the wheel.
Let’s start.
Set function returns a set object from an iterable (e.g. a list). Set is an unordered sequence of unique values. Thus, unlike lists, sets do not have duplicate values. One use case of sets is to remove duplicate values from a list or tuple. Let’s go over an example.
list_a = [1,8,5,3,3,4,4,4,5,6]set_a = set(list_a)print(list_a)print(set_a)[1, 8, 5, 3, 3, 4, 4, 4, 5, 6]{1, 3, 4, 5, 6, 8}
As you can see, duplicates are removed and the values are sorted. Sets are not subscriptable (because of not keeping an order) so we cannot access or modify the individual elements of a set. However, we can add elements to a set or remove elements from a set.
set_a.add(15)print(set_a){1, 3, 4, 5, 6, 8, 15}
Enumerate provides a count when working with iterables. It can be placed in a for loop or directly used on an iterable to create an enumerate object. Enumerate object is basically a list of tuples.
cities = ['Houston', 'Rome', 'Berlin', 'Madrid']print(list(enumerate(cities)))[(0, 'Houston'), (1, 'Rome'), (2, 'Berlin'), (3, 'Madrid')]
Using with a for loop:
for count, city in enumerate(cities):   print(count, city)0 Houston1 Rome2 Berlin3 Madrid
Isinstance function is used to check or compare the classes of objects. It accepts an object and a class as arguments and returns True if the object is an instance of that class.
a = [1,4,3,5,5,7]b = set(a)print(isinstance(a, list))print(isinstance(b, list))TrueFalse
We can access the class of an object using type function but isinstance comes in hand when doing comparisons.
Len function returns the length of an object or the number of items in an object. For instance, len can be used to check:
Number of characters in a string
Number of elements in a list
Number of rows in a dataframe
a = [1,4,3,5,5,7]b = set(a)c = 'Data science'print(f'The length of a is {len(a)}')print(f'The length of b is {len(b)}')print(f'The length of c is {len(c)}')The length of a is 6The length of b is 5The length of c is 12
As the name clearly explains, sorted function is used to sort an iterable. By default, sorting is done in ascending order but it can be changed with reverse parameter.
a = [1,4,3,2,8]print(sorted(a))print(sorted(a, reverse=True))[1, 2, 3, 4, 8][8, 4, 3, 2, 1]
We may need to sort a list or pandas series when doing data cleaning and processing. Sorted can also be used to sort the characters of a string but it returns a list.
c = "science"print(sorted(c))['c', 'c', 'e', 'e', 'i', 'n', 's']
Zip function takes iterables as argument and returns an iterator consists of tuples. Consider we have two associated lists.
a = [1,3,2,4]b = ['Rome','Houston','Madrid','Berlin']print(list(zip(a,b)))[(1, 'Rome'), (3, 'Houston'), (2, 'Madrid'), (4, 'Berlin')]
The iterables do not have to have the same length. The first matching indices are zipped. For instance, if list a was [1,3,2,4,5] in the case above, the output would be the same. Zip function can also take more than two iterables.
I did not want to separate any and all functions because they are kind of complement of each other. Any returns True if any element of an iterable is True. All returns True if all elements of an iterable is True.
One typical use case of any and all is checking the missing values in a pandas series or dataframe.
import pandas as pdser = pd.Series([1,4,5,None,9])print(ser.isna().any())print(ser.isna().all())TrueFalse
Range is an immutable sequence. It takes 3 arguments which are start, stop, and step size. For instance, range(0,10,2) returns a sequence that starts at 0 and stops at 10 (10 is exclusive) with increments of 2.
The only required argument is stop. Start and step arguments are optional.
print(list(range(5)))[0, 1, 2, 3, 4]print(list(range(2,10,2)))[2, 4, 6, 8]print(list(range(0,10,3)))[0, 3, 6, 9]
Map function applies a function to every element of an iterator and returns an iterable.
a = [1,2,3,4,5]print(list(map(lambda x: x*x, a)))[1, 4, 9, 16, 25]
We used map function to square each element of list a. This operation can also be done with a list comprehension.
a = [1,2,3,4,5]b = [x*x for x in a]print(b)
List comprehension is preferable over map function in most cases due to its speed and simpler syntax. For instance, for a list with 1 million elements, my computer accomplished this task in 1.2 seconds with list comprehension and in 1.4 seconds with map function.
When working with larger iterables (much larger than 1 million), map is a better choice due to memory issues. Here is a detailed post on list comprehensions if you’d like to read further.
towardsdatascience.com
List is a mutable sequence used to store collections of items with same or different types.
a = [1, 'a', True, [1,2,3], {1,2}]print(type(a))<class 'list'>print(len(a))5
a is a list that contains an integer, a string, a boolean value, a list, and a set. For this post, we are more interested in creating list with a type constructor (list()). You may have noticed that we used list() throughout this post. It takes an iterable and converts it to a list. For instance, map(lambda x: x*x, a) creates an iterator. We converted to a list to print it.
We can create a list from a range, set, pandas series, numpy array, or any other iterable.
import numpy as npa = {1,2,3,4}b = np.random.randint(10, size=5)c = range(6)print(list(a))[1, 2, 3, 4]print(list(b))[4, 4, 0, 5, 3]print(list(c))[0, 1, 2, 3, 4, 5]
Thank you for reading. Please let me know if you have any feedback.