How To Filter A List In Python

A list is a special kind of data structure which contains heterogeneous or different types of  data elements. A list is ordered, changeable or mutable in nature. A list may contain duplicate items or elements. In python, a list is created with variable name and square braces i.e. listname=[]. A syntax and example to  create a  list is given below.

 Syntax:       

                listname = [element1, element2,…..elementN]

Example:

               Person=[‘Jhon’, ‘Male’,‘NewYork’, 37, 1.23 ]

An above example shows creating a list named as Person which contains personal information such as name, gender, city, age and height. A python code to create a list is demonstrated below.

Python Code to create a List:

# Python code to create a list
Person=['Jhon', 'Male','NewYork', 37, 1.23 ]      # Here Person list is created
print(Person)

Output:

['Jhon', 'Male', 'NewYork', 37, 1.23]

Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named Person is created which contains the name, gender age and heights.
  • Elements of the list are displayed.

Why Is There A Need To Filter A List In Python

In python, a list may contain variable size data items. To process a specific data item or set of data items, we need to filter the desired list. With the help of filtering data elements, we can access data on the basis of different criteria. It also allows performing different numerical operations.        

Approaches: How to filter a List

To filter elements of a list, several approaches will be used. There are some approaches which are listed below. 

  • Approach-1: Index and Index Range i.e. Slicing
  • Approach-2: Loop Method 
  • Approach-3: List Comprehension
  • Approach-4: Pattern Matching: match() Method
  • Approach-5: Filter() Method

Approach-1: Index and Index Range i.e. Slicing

In python, lists support both positive and negative indexes. Positive index starts from 0 to length-1 and negative index starts from 1 to length of list.  With the help of positive and negative indexes, we can filter elements of the list. 

Filter list element using positive index  : Following code demonstrates filtering of list elements using positive index or range of indexes i.e. slicing.

# Python code to filter a list using positive index
Employee=['James', 'HR','HTC', 40, 15.50 ]  	# Here Employee list is created
print(Employee[0])
print(Employee[3])  	 
print(Employee[0:3])

Output:

James
40
['James', 'HR', 'HTC']

 Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named Employee is created which contains the name, department, company, age and salary of employees.
  • The first and fourth element is displayed separately using index value. It also displays the range of  elements from the list using a slice of list index. It excludes the end index value or element  in the slice.

Filter list element using negative index  : Following code demonstrates filtering of list elements using negative index or range of indexes i.e. slicing .

# Python code to filter a list using negative index
Employee=['James', 'HR','HTC', 40, 15.50 ]      # Here Employee list is created
print(Employee[-4])        # It display second element of a list Employee i.e. HR
print(Employee[-2])        # It display fourth element of a list Employee i.e. 40   
print(Employee[-4:-2])        # It display range of elements from the list i.e. HR, HTC   

Output:

HR
40
['HR', 'HTC']

 Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named Employee is created which contains the name, department, company, age and salary of employees.
  • The second and fourth element is displayed separately using index value. It also displays the range of  elements from the list using a slice of list index. It excludes the end index value or element in the slice

Approach-2: Loop Method

Loop method is another way to filter elements of the list. Here, we demonstrate filtering of list elements using a for loop. Python code to demonstrate for loop to filter list elements given below.

# Python code to demonstrates loop method i.e. for loop 
Person=['Jhon', 'Male','NewYork', 37, 1.23 ]      # Here Person list is created
for i in Person:
  print(i)

Output:

Jhon
Male
NewYork
37
1.23

 Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named Person is created which contains the name, gender age and heights.
  • Elements of the list are displayed using a for loop.

The loop method also filters elements of the list in another way. It can also filter list elements using index values in the for loop which start from 0 to  end with list length in range function. Range function excludes the last index value i.e list length. Python code to demonstrate this form given below.  

# Python code to demonstrates range function in loop method i.e. for loop 
Person=['Jhon', 'Male','NewYork', 37, 1.23 ]      # Here Person list is created
for i in range(0,len(Person)):     # For loop to filter list elements
  print(Person[i])

Jhon
Male
NewYork
37
1.23

Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named Person is created which contains the name, gender age and heights.
  • Elements of the list are displayed using index value and range function in the for loop.

Approach-3: List Comprehension Method

List comprehension method allows programmers to write single line code to filter list elements using a for loop. Python code to demonstrate list comprehension method given below.

Python code:

# Python code to filter a list using list comprehension method
Employee=['James', 'HR','HTC', 40, 15.50 ]      # Here Employee list is created
e= [i for i in Employee]     # It implement list comprehension method
print(e)
e1= [i for i in Employee if i=='HR']     # It implement list comprehension method with if conditional statement
print(e1)

Output1:

['James', 'HR', 'HTC', 40, 15.5]

Output2:

['HR']

 Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named Employee is created which contains the name, department, company, age and salary of employees.
  • The entire list is displayed as output 1. It also displays the department of employee using if conditional statement in the list comprehension.

Approach-4: Pattern Matching-match() method

When we want to filter a string type list which contains only a set of strings then the match() method is suitable. It works on string patterns. For example, we can filter out employee names which start with an alphabet ‘J’  from the employee list. The python code to demonstrate the match() method is given below.

# Python code to filter a list using match() method
import re     # It import regular expression library 
Employee=['James', 'Kalis','Jhonson', 'Marry', 'Luise' ]      # Here Employee list is created
alphabet="J.*"             # It define pattern which match with the list elements 
for i in Employee:         # It define for loop
  if re.match(alphabet, i): # It matches the pattern with every element of the list. If it matched then display  
    print(i) 

Output:

James
Jhonson

Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • Imports the regular expression i.e. re  library.
  • A list named Employee is created which contains the names of employees.
  • Pattern for the match is defined with the ‘J’ alphabet.
  • It displays the names of employees whose names start from the ‘J’ alphabet.

Approach-5: Filter() Method

With the help of an in-built function named filter(), we can filter elements of the list. For example we want to filter the salary of an employee whose salary is greater than 50000 then filter functions may be used. The python code to demonstrate the filter() function is given below.

# Python code to filter a list using filter() method
EmpSal=[15000,35000,45000,55000,40000,60000,520000,48000]      # Here Employee salary list is created
def Salary(EmpSal):        # A Function is created using to filter employee salary
  if EmpSal>50000:
    return True
sal=filter(Salary,EmpSal)    # A filter function is called
print(list(sal))             # A filtered list element is displayed

Output:

[55000, 60000, 520000]

Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named EmpSal is created which contains the salary of employees.
  • A function is created to filter salary i.e. greater than 50000. Then the filter function is called.
  • It displays the salary of employees whose salary is greater than 50000.

Best Approach- List Comprehension Method

 List comprehension method is the best approach to filter list elements. It allows programmers to write entire code in a single line. It has the following features.

  • It can filter variable size lists or unlimited elements of a list.
  • It allows defining of loops and conditional statements in its structures.
  • It is also able to filter heterogeneous lists.

Sample problem related to filtering a list in Python

Sample Problem-1: Using Approach-1

Problem Definition: Create a python code that demonstrates creation of a list which contains product names. After this answer the following question.

Question- To  display the name of the third and fifth product.

Solution (Python Code):

# Python code to filter a list using approach-1 i.e. index value or index slice 
ProductName=['Shirt','Jeans','Cap', 'SunGlass','Shoes','Perfume']
print(ProductName[2])
print(ProductName[4])
print(ProductName[-4])
print(ProductName[-2])

Output:

Cap
Shoes
Cap
Shoes

 Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named ProductName is created which contains the name of products
  • The name of the first and fifth product is displayed using positive and negative indexes.

Sample Problem-2:Using Approach-2

Consider sample problem-1 and answer the following question.

Question: To display the name of all products.

# Python code to filter a list using approach-2 i.e. loop method 
ProductName=['Shirt','Jeans','Cap', 'SunGlass','Shoes','Perfume']
for i in ProductName:
  print(i)

 Output:

Shirt
Jeans
Cap
SunGlass
Shoes
Perfume

  Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named ProductName is created which contains the name of products
  • The name of all the products are displayed using the loop method.

Sample Problem-3: Using Approach-3

Consider sample problem-1 and answer the following question.

Question: Filter products in a given range i.e start from 0 to end with last indexes.

# Python code to filter a list using approach-3 i.e. list comprehension method 
ProductName=['Shirt','Jeans','Cap', 'SunGlass','Shoes','Perfume']
e= [ProductName[i] for i in range(0,len(ProductName))]
print(e)

Output:

['Shirt', 'Jeans', 'Cap', 'SunGlass', 'Shoes', 'Perfume']

  Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named ProductName is created which contains the name of products
  • The names of all the products that start from index 0 to end with last index are displayed using the list comprehension.

Sample Problem-4: Using Approach-4

Consider sample problem-1 and answer the following question.

Question: To display the name of all products whose name start with ‘S’ alphabet

# Python code to filter a list using approach-4 i.e. match() method 
import re     # It import regular expression library 
ProductName=['Shirt','Jeans','Cap', 'SunGlass','Shoes','Perfume']      # Here Employee list is created
alphabet="S.*"             # It define pattern which match with the list elements 
for i in ProductName:         # It define for loop
  if re.match(alphabet, i): # It match pattern with every element of list. If it matched then display  
    print(i)  

Output:

Shirt
SunGlass
Shoes

Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named ProductName is created which contains the name of products
  • The names of all the products whose names start with ‘S’ alphabet are displayed using the match() method.

Sample Problem-5: Using Approach-5

Create a list which contains the age of employees and answer the following question.

Question: To display the age of all employees whose age is greater than 25.

# Python code to filter a list using approach-5 i.e. filter() method
EmpAge=[25,24,38,34,40,31,27,45,56,29]      # Here Employee age list is created
def Age(EmpAge):        # A Function is created using to filter employee age
  if EmpAge>25:
    return True
age=filter(Age,EmpAge)    # A filter function is called
print(list(age))             # A filtered list element is displayed

Output:

[38, 34, 40, 31, 27, 45, 56, 29]

Code Explanation:

  • Single line comment tells the goal of the program to programmers.
  • A list named EmpAge is created which contains the age of employees
  • The age of all the employees whose age is greater than 25 are displayed using filter() function.

Conclusion

In python, lists are ordered, mutable and heterogenous data structures. It may contain duplicate elements. List elements may be filtered on the basis of different criteria using numerous approaches such as index value, loop method,  list comprehension, pattern matching and filter function.

Among all, filter list elements through a list comprehension method is a convenient and effective approach. In this, all approaches of data filtering have presented with code, output and its explanation.