• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Indicator Function – Apply Indicator Function to List of Numbers

Python Indicator Function – Apply Indicator Function to List of Numbers

February 8, 2022 Leave a Comment

In Python, we can define and apply indicator functions easily. To apply an indicator function to a list of numbers using Python, the easier way is with list comprehension.

list_of_nums = [10,-4,2,0,-8]

indicator = [1 if x > 0 else 0 for x in list_of_nums]

print(indicator)

#Output:
[1, 0, 1, 0, 0]

An indicator function maps a set of numbers to the values 0 and 1.

In Python, we can define our own indicator functions and apply them to lists of numbers.

One such indicator function is the function that if a number is positive, we should return 1, and if the number is not positive, then we return 0.

To apply this indicator function to a list of numbers, we can use list comprehension as shown in the following Python code.

list_of_nums = [10,-4,2,0,-8]

indicator = [1 if x > 0 else 0 for x in list_of_nums]

print(indicator)

#Output:
[1, 0, 1, 0, 0]

We can get the same result if we use a for loop, but list comprehension is easier to read and is less code.

list_of_nums = [10,-4,2,0,-8]

indicator = []

for x in list_of_nums: 
    if x > 0:
        indicator.append[1]
    else:
        indicator.append[0]

print(indicator)

#Output:
[1, 0, 1, 0, 0]

Applying a Complicated Indicator Function to a List in Python

If we have a more complicated indicator function, most of the time, it will be easier to code up the function and then use list comprehension.

For example, let’s say we have an indicator function which should be 0 if the number is less than -10, 1 if the number is between -10 and 10, and is 0 if the number is greater than 10.

While it’s possible to do this in one line, it’s easier to follow if we define a function which defines the indicator function, and then we can apply it to each element of a list.

Below is the indicator function in Python for our example.

def indicator_function(num):
    if num < -10 or num > 10: 
        return 0
    else:
        return 1

We can apply this function now on a list of numbers.

list_of_nums = [15,-4,12,0,-8]

indicator = [indicator_function(x) for x in  list_of_nums]

print(indicator)

#Output:
[0, 1, 0, 1, 1]

Hopefully this article has been useful for you to understand how to use list comprehension to apply an indicator function in Python.

Other Articles You'll Also Like:

  • 1.  How to Create Block and Multi-Line Comments in Python
  • 2.  Python math.factorial() function – Calculate Factorial of Number
  • 3.  Remove First and Last Character from String Using Python
  • 4.  Check if Variable is Integer in Python
  • 5.  How to Multiply All Elements in List Using Python
  • 6.  Using Python to Flatten Array of Arrays
  • 7.  How to Write Pickle File to AWS S3 Bucket Using Python
  • 8.  Perform Reverse Dictionary Lookup in Python
  • 9.  Find Quotient and Remainder After Division in Python
  • 10.  Convert String to Float with float() in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x