How To Filter Python List, Dictionary, Array, String List, Object Tutorial with Examples? – POFTUT

How To Filter Python List, Dictionary, Array, String List, Object Tutorial with Examples?


Python programming language provides filter() function in order to filter a given array, list, dictionary, or similar iterable struct.  filter() function can be used to create iterable by filtering some elements of the given data.

Python Filter Function Syntax

filter() function has the following syntax. FUNCTION is the function name we will use to test the given dataset and create a new iterable list. ITERABLE is the data we will check with our function and filter.

filter(FUNCTION, ITERABLE)

Filter List

The most popular usage of the filter() function is using Python List. We will provide a list which will be filtered with the given function. In this example, we will provide the list named numbers  and use oddF'lter() function in order to filter numbers.

#!/bin/python3

numbers=[1,2,3,4,5,6,7,8,9]

def oddFilter(number):
   if(number%2==0):
      return True
   else:
      return False

odd_numbers=filter(oddFilter,numbers)

for number in odd_numbers:
   print(number)
Filter List
Filter List

We will provide the list named numbers. This list contains numbers from 1 to 9. We will create a filter function named oddFilter() which will filter for odd numbers in the given list and return True if the given element is odd or return False if the given element is even. Then we will add odd numbers to a new list named  odd_numbers.

Filter Dictionary

Dictionaries can be also filtered with the filter() function. We can filter dictionaries according to their key or value for each element. In this example, we will filter the dictionary named names.

#!/bin/python3

names={1:"ismail",2:"ali",3:"ahmet",4:"elif",5:"ecrin"}

filtered_dict = dict(filter(lambda item: item[0]%2==0 , names.items()))

for item in filtered_dict.items():
   print(item)
Filter Dictionary
Filter Dictionary

Filter String List

We can also use filter() function in order to filter the given string list. In this example, we will look for strings which contains i letter.

#!/bin/python3

names=['ismail','ali','ahmet','elif','ecrin']

def filterString(name):
   if 'i' in name:
      return True
   else:
      return False

filtered_names=filter(filterString,names)

for name in filtered_names:
   print(name)
Filter String List
Filter String List

Filter Using Lambda

Lambda is very useful when we want not to use a function. As  filter() function requires a function we can skip defining a new function and use lambda like a function. We will filter those strings which contain the letter i.

#!/bin/python3

names=["ismail","ali","ahmet","elif","ecrin"]

filtered_names = filter(lambda item: 'i' in item , names)

for item in filtered_names:
   print(item)

LEARN MORE  Python Lambda - Anonymous Function Tutorial with Examples

3 thoughts on “How To Filter Python List, Dictionary, Array, String List, Object Tutorial with Examples?”

  1. In the “filter dictionary” section, the code has en error (probably because of the HTML rendering). It misses out two zeros (‘0’). One inside the brackets and another after the equal operator ‘==’)

    Reply
  2. LOL…. I always wonder how such things come into being.
    You’ve documented in the list filtering section that you are going to filter a list of numbers for the odd ones. You’ve got the output shown right there.

    Does the person that created the web page not understand the code? Do they not read it? Does the person that created the code not know the difference between odd and even? Did they never run the code they created?

    How does this happen?

    Reply

Leave a Comment