10 Useful Tips in Python Coding

Daihong Chen
5 min readOct 16, 2020

This this post, I am going to share 10 useful codes or functions in python for data precessing.

  1. Reverse a list or string

Python has a unique function of -1 to retrieve the last element from a list or string, or to reverse the string or list.

Reversing can be also used to check if a word is palindrome. It is simple to understand because if the reversed string is the same as the original string, then this string is palindrome.

2. Find the unique elements. Yes, you might have thought about it already, using set() function. The set() function in python will return the unique elements automatically! Nevertheless, if you are asked to find the unique elements or string in a list without using functions, here is the algorithm to make it happen. Create a new dictionary — hashmap is very useful when need to identify unique words or duplicates. It is also often asked in interviews as well. You can try this out, it is fun!

3. The third one is exchanging values of variables. It is super simple, but, it is very useful concept and function. For example, one leetcode problem could be solved by using exchange values easily. Let take a look:

It is simple, right? Now, we will check how it can be used to solve a more challenging problem.

Problem:

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

You may return any answer array that satisfies this condition.

Example 1:

Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

Solution: by checking if A[i]/2 == 0 to identify even numbers, if yes, then the even number will exchange with the current element and to move forward.

4. List comprehension. List comprehension is a one line code with a for loop in the list.

5. Dictionary combination. Dictionary is a very useful data structure because it has key and values, which is easy to search and locate. Sometimes, we need to combine a few dictionaries together. The following simple code makes it happen! If the dictionaries have the same key, then the value of that key in the last dictionary will be used. For example, the following shows three dictionaries, and the last one that have the value of 3 to the key ‘banana’ is used in the combined dictionary.

6. The function of Counter() from collections. Counter automatically generates the elements and their corresponding frequencies in a list or string. Very easy to use in data exploration and processing. Counter() function also has a sub function most_common(), with the parameter of the number of the most common elements you set, can quickly provide the most common elements and their frequency.

Nevertheless, you may be asked to write an algorithm to check the frequency without using a function. It is the time the hashmark dictionary comes in handy!

7. Following the last one, using the Counter function to check anagrams. Anagram means two words use the same elements but in different orders. It can be thought as we have same elements, and the same number of each element in the two words. Obviously, if the elements and their frequencies are the same, they are anagrams.

8. Try, except, else, continue, finally: With the help of try-except and try-except-else, we can avoid many unknown problems that could arise from the code.

9. Random sampling and secured random sampling. In machine learning, sometimes, we need to subsample from a big dataset due to computational cost. Random sampling is useful in this situation. Random library has a function random.sample that is often used for sampling. The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets. In particular, secrets should be used in preference to the default pseudo-random number generator in the random module, which is designed for modeling and simulation, not security or cryptography.

10. The last coding tip I want to share is my favorite, the enumerate(). Enumerate() is a built-in function of Python. Its usefulness can not be summarized in a single line. Yet most of the newcomers and even some advanced programmers are unaware of it. It allows us to loop over something and have an automatic counter. Here is an example:

Hope you enjoy reading it!

--

--

Daihong Chen

Data Science, Machine Learning, Data Visualization, and Climbing.