As you already know Python is the best programming language for the data science. Python packages like Pandas,Numpy scikit-learn and others are popular Python modules for data science. We use it in while writing code for the machine learning or deep learning. But one thing we have realized that while writing we have to deal with most of the tasks on the lists or dictionaries. And Iterating these lists are not efficient as compared to Python Comprehension.Thus in the entire intuition of “ How to “ you will learn how to do comprehension in python.

## How to do a list comprehension in python?

For better understanding let’s take a basic example. Suppose I want to square the numbers from a given list. I can use the map function and lambda function. Like this

```
numbers = [1,2,3,4,5,6,7]
square_of_numbers = list(map(lambda n: n**2,numbers))
print(square_of_numbers)
```

But I can use the list comprehension to increase efficiency and readable. Like this

```
numbers = [1,2,3,4,5,6,7]
square_of_numbers = [n**2 for n in numbers if n >3]
print(square_of_numbers)
```

The square bracket represents that list comprehension. and all the logics are inside the [ ]. It will finally generate the list of the square of the numbers for all the numbers that are greater than 3.

Output

### How can you use it in Data Science?

You can use it in Data Science like to change the categorical variable to a numerical variable. Suppose I have a Gender column in the data frame and I want to change into numerical form that is Male to 1 and Female to 0. Use the following code.

```
gender = ["Male","Female","Male","Female","Male","Female","Male","Male","Female","Female","Male","Female","Male","Female","Male","Male"]
df = pd.DataFrame(gender,columns=["gender"])
gender_numeric = [1 if g =="Male" else 0 for g in df["gender"] ]
print(gender_numeric)
```

## How to do list comprehension in python for the dictionary?

You can also use comprehension to build a dictionary. For example, let’s say I have a list of numbers and I am assigning true to all the numbers that are greater than 5.

```
numbers = [1,2,3,4,5,6,7]
test = {n : True if n > 5 else False for n in numbers}
print(test)
```

Another example I can take is that let’s say I have two dictionaries that have names and their corresponding age. And I want to merge them then I will use the following code for that. Use the Following Code.

```
dict1 = {"Abhishek":26,"Monae":20,"John":30}
dict2 ={"Rohan":21,"Maya":36}
merged_dict = {i:j for name in (dict1,dict2) for i,j in name.items()}
print(merged_dict)
```

Output

#### Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.