Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Learning List Comprehension in Python

Learning List Comprehension in Python

 June 18  | 0 Comments

The List is one of the versatile collection Data Types in Python. It is similar to an array but stores multiple type data in a single variable.

We have discussed on the List and other collection types in our earlier blogs. We recommend our readers to go through this collection details from below link blog.

Free Step-by-step Guide To Become A Data Scientist

Subscribe and get this detailed guide absolutely FREE

Python_Basics

In this blog, we will learn the collection “List” and “List comprehension in Python in more detail.

The List is a collection of Data which is ordered, means its index are fixed and is mutable, i.e, items inside a list can be changed at any point of time.

The items inside a list are separated by ‘,’ and are enclosed within ‘[ ]’.

Eg: list1 = [ 1, 14, 8, 3, 23]

list2 = [ “Red”, “Yellow”, “Green”, “Blue”]

We can perform numerous operations on the list. Let us understand each operation with an example.

ACCESSING ITEMS FROM A LIST

We can access items from a list by referring to its index number. Indexing of a list starts from 0 to (length-1).

To print the first and last item of a string.

list = ["Red", "Yellow", "Green", "White"]
print(list)
print("Printing the first item of the list: ",list[0])
print("Printing the last item of the list: ", list[-1]) #negative indexing is done from backward

CHANGING LIST ITEM  VALUE

Since items are mutable, we can change the value of any of the items as:

list = ["Red", "Yellow", "Green", "White"]
list[1] = "Pink" #inserting string ‘Pink’ at index 1
print("The updated list:", list)

INSERTING ITEMS TO A LIST

We can insert new items in an existing list with the help of append() and insert() method.

The difference between the two methods is append() method adds an item at the end of the list and insert() method adds an item at a specified index.

list = ["Red", "Yellow", "Green", "White"]
list.append("Purple") #appending string ‘purple’ at the end of the list
print(list)
list.insert(2, "Black") # inserting string ‘Black’ at the 2nd index
print(list)

DELETING ITEMS FROM A LIST

We can delete item/s from a list by a number of methods like pop(), clear(), remove() and ‘del’ keyword.

pop() method removes the last item of a list or at the specified index.

clear() method empties the list.

remove() method removes the specified item.

The del keyword removes an item at a specified index or deletes the list completely.

list = ['Red', 'Yellow', 'Black', 'Green', 'White', 'Purple']
list.pop() #removes the last element
print(list)
list.remove("Yellow") #removes the specified item, also note that it is case-sensitive
print(list)
del list[0] #delete item at 0th index i.e, the first item
print(list)
list.clear() #clears the list
print(list)

LENGTH OF LIST

We can determine the length of the element, i.e., the number of items in a list by len() method.

list = ['Red', 'Yellow', 'Black', 'Green', 'White', 'Purple']
len(list)

LIST SLICING

List slicing is the method of splitting a list into its subset. We do this with the help of the indices of the list items.

list1 = [ 31,2,16,80,3,29,19,43,61,50]
print(list1[:]) #printing all the items of a list
print(list1[:5]) #printing items from starting (0th index) to 5th index, excluding item at the 5th index
print(list1[2:6]) # printing items from the second index(16) to the 6th index, excluding item at the 6th index
print(list1[3:-1]) #printing items from the third index(80) to the last index, excluding item at the last index

During slicing when we specify two index, the last indexed item (n) is usually excluded and item at (n-1)th position is taken.

LOOPING THROUGH A LIST

When we have a number of items in a list, we can loop through the list with the help of ‘for’ loop as shown below:

list1 = [ 31,2,16,80,50]
for x in list1: #looping through the list ‘list1’ and printing each item at a time
    print(x)

Another example of list looping where we have added 5 to each element in the list.

list1 = [ 31,2,16,80,50]
for x in list1:
    x = x + 5
    print(x)

LIST COMPREHENSION

List comprehensions are Python functions that are used to create new lists, sets, dictionaries, etc.using lists that have already been created.

It reduces loops and makes code easier to read.

As list comprehensions return lists, they consist of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.

The comprehension is usually shorter, more readable, and more efficient.

Types of comprehension:

  •  List

[ i*2 for i in range(3) ]

  • Set

{ i*2 for i in range(3)}

  • Dictionary

d = {key: value for item in sequence …} </n>

{ i: i*2 for i in range(3)}

We will understand the concept of List Comprehension in a better way by below a number of examples.

1. Squaring values of a list and a set

def square(list):
    return [i ** 2 for i in list]

square(range(0,11))

In the above program, we have created a function with name ‘square’ and passed a variable ‘list’ as the argument. Then we implemented list comprehension to square each value of the list. And then we defined the list to be in the range 0 to 10 as the last range value is excluded. Therefore the output would be like:

setvalue ={0,1,2,3,4,5}

square = [i ** 2 for i in setvalue]
print(square)
type(square)

In the above program, a set of values is stored in a variable called ‘setvalue’ from 0 to 5. We implemented list comprehension to square each value in the setvalue and stored it in variable ‘square’. To know the collection type of ‘square’ we can check it by using type(square) in our code. Therefore the output would be something like this.

2. Converting temperature from Centigrade to Fahrenheit.

ctemps = [17.1, 22.3, 18.4, 19.1] #temperature in celsius

ftemp = [((i * 9/5) + 32) for i in ctemps] #applying formula to convert each value of ctemps into fahrenheit

print(ftemp) #printing ftemp

3. Working with Strings

Given is a string containing the names of people both first and last name. We will be performing two operations on the list, first to separate only the last name and second to print the name in reverse order.

The above operations will be carried out by using the split() method.

split() method returns a list of strings after breaking the given string by the specified separator (i.e., by which the list is separated).

syntax: str.split(delimeter)

It returns a list of strings after breaking the given string by the specified separator.

names = ["Isaac Newton", "Albert Einstein", "Niels Bohr", "Marie Curie",
"Charles Darwin", "Louis Pasteur", "Galileo Galilei", "Margaret Mead"]
x = [i.split()[1] for i in names] #splitting wherever there is space and at index 1 i.e., the last name
x
x = [i.split()[::-1] for i in names] # splitting where there is space and reversing the order using [::-1]
x

4. Printing cubes of first 10 natural numbers

def cube(list):
    return [i ** 3 for i in list]

cube(range(0,10))

In the above program, we have created a function named cube and passed a variable list as the argument. Then list comprehension is implemented where for each value in the list its cube is calculated. The range for the list is given from 0 to 10 where 10 being excluded.

Therefore the output would be:

5. Finding common words in 2 lists 

lst_1= "I love Python coding"
lst_2= "I am learning Data Science with Python"

[s for s in lst_1.split()  if s in lst_2.split()]  #checking for every word present in lst_1(separated by spaces) if it is also present in lst_2

6. List Comprehension to get the given output

l1=[1,2,3,4,5] 

l2=[4,5,6,7]

  • output 1 : [1,2,3]
  • output 2 : [6,7]
  • output 3 : [4,5]
l1=[1,2,3,4,5] 
l2=[4,5,6,7]

lc1=[i for i in l1 if i<4] #checking for numbers less than 4 in list 1
lc1
lc2 = [i for i in l2 if i>5] #checking for numbers greater than 5 in list 2
lc2
lc3 = [i for i in l2 if i<6] #checking for numbers less than 6 in list 2
lc3

7. Summing the numbers when two dice are rolled

Note: the sum of two numbers in tuples should be more than 7.

x =  [(i,j) for i in range(0,7) for j in range(0,7)  if i+j > 7] #i and j are the two numbers that would come upon respective dice.
x

8. For given input , produce given output

inp = “Hello Python World”

  • output 1 : ‘dlroW nohtyP olleH’
  • output 2 : ‘olleH nohtyP dlroW’
  • output 3 : ‘World Python Hello’
inp = "Hello Python World"
out1 = inp[::-1] #reverses the whole string
out1
out2 = ' '.join([x[::-1] for x in inp.split(' ')]) #reverse the string at its respective position that is separated by space
out2

The join() method is a string method and returns a string in which the elements of the sequence have been joined by str separator.

out3 = ' '.join([x for x in inp.split(' ')[::-1]])
out3

9. Removing vowels from list

inp = "HellO python wOrld !"
out1 = ‘’.join([i for i in inp if i.lower() not in ['a','e','i','o','u']]) #checking words if it contains vowels or not
out1

10.Replacing missing spaces in a string with the least frequent character

  • Input : ‘dbc deb abed gade’
  • Output: ‘dbccdebcabedcgade’
my_str = 'dbc deb abed gade' #we have this given input 

import pandas as pd #importing pandas libraries for creating series
ser = pd.Series(list('dbc deb abed gade')) #created 1-D array of indexed data
ser
freq = ser.value_counts() #calculating the frequency count of each data and printing it
print(freq)
least_freq = freq.dropna().index[-1] #as ‘c’ and ‘g’ has occurred least number of times, we will take the last one i.e., c
least_freq
out1 = "".join(ser.replace(' ', least_freq)) #we will join each letter without any space and will replace spaces with ‘c’
out1

11. Create a numpy 4*4 array and get the given output

  • Input: array([ [ 0, 1, 2, 3],
    [ 4, 5, 6, 7],
    [ 8, 9, 10, 11],
    [12, 13, 14, 15] ])
  • Output = array([ [ 4, 6],
        [12, 14] ])
import numpy as np #imported numpy libraries for creating array
x = np.arange(16).reshape(4,4) #creating 4x4 matrix
x
x[1::2,::2] #slicing alternate row and third column, [::2] is used to access every alternate element

12. Creating a DataFrame and performing operations on it

In this example, we will be creating and Data Frame and check for some given condition.

import pandas as pd  
df = pd.DataFrame({'DateOfBirth': ['1986-11-11', '1999-05-12', '1976-01-01','1986-06-01', '1983-06-04', '1990-03-07','1999-07-09'],
                   'Name':['Jane', 'Pane', 'Aaron', 'Penelope', 'Frane','Christina', 'Cornelia'],
                   'State': ['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX']})
df

In the above code, we have imported the Pandas library and created a DataFrame with three columns viz, DateOfBirth, Name, and State. Therefore the output will be:

Now we have to find all rows in the above DataFrame where ‘Name’ contains “ane” or ‘State’=”TX”

df1 = df[(df['Name'].str.contains("ane")) | (df['State'].str.contains("TX"))]
df1

df[‘Name’] selects the Name column of the DataFrame. df[‘Name’].str allows us to apply string methods (e.g., lower, contains) to the DataFrame.

df[‘Name’].str.contains(‘ane’) checks each element of the Column as whether it contains the string ‘ane’ as a substring. The result is a Series of Booleans indicating True or False.

df[df[‘Name’].str.contains(‘ane’)] applies the Boolean ‘mask’ to the dataframe and returns a view containing appropriate records.

This brings us to the end of the blog. Hope this blog helped you in learning List Comprehension in Python from scratch. You can refer to the blogs on Python Libraries to understand List Comprehension in a better way.

>