I have been using Python since I took my first step into Data Science. Although I excessively focus on the third party libraries such as Pandas and Scikit-learn, it is of vital importance to learn base Python as well.
Python is a great language for various reasons. It is easy to learn and understand. You can read Python code as you are reading plain English. Python also leads to quick and easy development processes which I think is a motivation booster.
On the other hand, Python has some features that we need to pay extra attention to. Otherwise, we might make some mistakes without noticing. I have actually made such mistakes in my journey of learning Python.
In this article, we will go over 4 Python features that have a potential to cause you trouble if not used properly.
1. Methods that work in place
Python provides several methods to modify built-in data structures. For instance, the sort method can be used for sorting the items in a list.
However, some of these methods work in place which means they do not return anything. A potential mistake with these methods is to make variable assignments with them.
Consider we have the following list and need to sort it.
mylist = [1, 4, 2, 10, 5]
mysortedlist = mylist.sort()
Let’s check the order of items in the sorted list.
print(mysortedlist)
None
It does not actually have any value because the sort method sorts the items in the original list and does not return anything.
print(mylist)
[1, 2, 4, 5, 10]
Be careful when you are using methods that work in place. Otherwise, you will end up having variables with no value.
If you do not want to modify the original list, you can always use the sorted function which does not change the original one but returns a sorted version of it.
mylist = ["d", "a", "c"]
mysortedlist = sorted(mylist)
print(mysortedlist)
['a', 'c', 'd']
2. Create an empty set
Set is a built-in data structure in Python. It is similar to a list but it only contains unique values.
mylist = [1, 1, 2, 3, 3, 4]
myset = set(mylist)
print(myset)
{1, 2, 3, 4}
The duplicates are removed while we convert a list to a set. Sets are represented as values in curly braces separated by commas. Thus, we should be able to create an empty set as below:
myset = {}
Let’s check the type of the myset variable.
type(myset)
dict
It is a dictionary, not a set. Dictionary is another built-in data structure and represented as key-value pairs in curly braces. Curly braces with no item in between result in a dictionary.
We can use the set function to create an empty set.
myset = set()
type(myset)
set
3. Create a tuple with a single item
Tuple is another built-in data structure in Python. It is represented as values in parenthesis and separated by commas.
mytuple = ("Jane","John","Matt")
type(mytuple)
tuple
Tuples are similar to lists but they are immutable. Thus, we cannot update the values in a tuple.
The append method does not work on tuples either. However, we can add tuples together to create another one.
mytuple = ("Jane","John","Matt")
mytuple + ("Ashley", "Max")
('Jane', 'John', 'Matt', 'Ashley', 'Max')
Creating a tuple with one item is a bit tricky. Although there is only one item, you should put a comma after it. Otherwise, you will end up having a string instead of a tuple.
mytuple = ("Jane")
print(mytuple)
Jane
type(mytuple)
str
Let’s do the same operation with a comma.
mytuple = ("Jane",)
print(mytuple)
('Jane',)
type(mytuple)
tuple
4. Multiple iterables in a list comprehension
List comprehension is one of the best Python features. It provides a way of creating lists based on other iterables such as sets, tuples, other lists, and so on.
List comprehensions can also be described as representing for and if loops with a simpler and more appealing syntax. They are also relatively faster than for loops though.
Here is a simple example.
a = [1, 2, 3]
mylist = [x**2 for x in a]
mylist
[1, 4, 9]
The new list contains the square of each item in list a. List comprehensions allow for using multiple items in different iterables. For instance, we can create a list based on two other lists.
Consider the following lists a and b.
a = [1,2,3]
b = [6,7,8]
I want to create a list that contains the products of items with the same index in lists a and b. Thus, the new list will have items 6, 14, and 24. The following list comprehension seems like a good solution for this task.
newlist = [x*y for x in a for y in b]
Let’s check the results.
newlist
[6, 7, 8, 12, 14, 16, 18, 21, 24]
It is not exactly what we need. Instead of multiplying the items with the same index (16, 27, and 3*8), all the items in the first list are multiplied with all the items in the second one. It is actually a cross product.
Python provides a great solution for our problem here: the zip function.
newlist = [x*y for x, y in zip(a,b)]
newlist
[6, 14, 24]
The zip function returns a zip object that contains tuples of matching items in both lists.
list(zip(a, b))
[(1, 6), (2, 7), (3, 8)]
We have covered 4 Python features that are not so obvious. They might cause you to make mistakes that are difficult to notice at first glance. However, such mistakes might have severe consequences especially if you are using them in a long script.
Thank you for reading. Please let me know if you have any feedback.