×

Filter List in Python

Python:

Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming language, as the syntax for it is very simple to write and easy to understand for a beginner programmer. It is used in many popular companies worldwide like google, uber, Instagram, amazon, etc. Python programming language is considered a general purpose; it is a high-level programming language that is not much difficult but easier to learn. Python programming language is rich in libraries that can be imported easily and used to perform many different operations. In the year 1989, Guido van Rossum is the one who introduced python programming language. Python is widely used in machine learning, and data science departments deal with heavy data. It is also used in web applications; web applications like the Django and Flask frameworks are created using python. The python programming language is straightforward to learn by anyone. Compared to any programming language, the syntax in python is much easier. Many colleges and institutions have introduced python in their syllabus so that the students need to learn python. The biggest advantage of the python programming language is that it has a good collection of libraries, which are widely used in machine learning, web frameworks, test frameworks, multimedia, image processing, and many more applications. 

Filter list in python

Lists in python:

In python, programming lists are considered to be a collection of things or items that are enclosed in square brackets ( [ ] ) and the items in the area by commas (, ). It is a sequence datatype that stores the collection of data. Also, tuples and strings are considered to be sequence data types. Now let us learn how to create a list in a python programming language. Let us look into a program to understand it in a better way.

Example code 

# # # creating a list in a python programming language
L = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# # # printing the created list
print( L )

Output

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

Lists can contain different datatypes, as it is not restricted to only integers; they can have integers and strings and can also have objects. These are considered to be mutable, that means can be changed even after declaration.

Filter list in python

The filter() method filters the required sequence of data with the help of the function, which tests whether or not each element present in the sequence is true. The syntax for the function is given by 

Syntax:

filter( function, sequence)

The parameters are function, sequence, and returns

Function: This parameter tests whether each element in the sequence is true or not.

Sequence: This parameter is needed to be filtered; this sequence can be of sets, lists, tuples, etc.

Returns: This parameter returns an iterator that is filtered

Now let us look into an example in which the function filters the vowels

Example program:

# # # defining a function
def func(var):
     Alpha = [ 'a', ' e ', ' i ', ' o ', ' u ']


if (var in alpha):
    Return True
else:
    Return False
# # # let's define the sequence as a list as we wanted to filter the list type of sequence data type
L = [ 'a', 'v', 'e', 'n', 'g', 'e', 'r', 's', 'a', 's', 's', 'e', 'm', 'b', 'l', 'e' ]
print(L)
# # # using the filter function to filter the vowels and consonants
Filter = filter(func, L)
print(" the filtered are:" )
for s in Filter:
    print(s)


Output

 [ ‘a’ , ‘v’ , ‘e’ , ‘n’, ‘g’, ‘e’, ‘r’, ‘s’ , ‘a’, ‘s’, ‘s’, ‘e’, ‘m’, ‘b’ , ‘l’, ‘e’ ]
a
e
e
a
e
e

It is generally used with lambda functions, which are used to filter lists, tuples, and sets.


Related Topics

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Python program to perform the arithmetic operation

Python program to perform the arithmetic operation This program will write a code to perform some basic arithmetic operations like addition, subtraction, multiplication, exponent, modulus, and division. Here we first need...

2 minutes read.

Data Distribution in python

Before discussing data distribution, we will discuss each word that is data and Distribution. What is meant by data? A collection of raw facts and figures is called data. The word "raw"...

18 minutes read.

Create a Table Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Python pycountry

What is Pycountry? Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations...

4 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Python map() function

Python map() function The map() function in Python returns an iterator that applies a function to every item of iterable, yielding the results. Syntax map(function, iterable, ...) Parameter function: It is a required parameter that represents the function to...

1 minute read.

Import py file in Python

In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module...

4 minutes read.

Python delattr() function

Python delattr() function The delattr() function in Python is used to delete the named attribute from the object, with the prior permission of the object. Syntax delattr(object, name) Parameter object : This parameter represents the object from which...

1 minute read.

Python PPTX

Python-pptx is a python library that enables the user to create and update PowerPoint (.pptx) presentations. It is used to create modified PowerPoint presentations from the db content that can...

10 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data...

3 minutes read.

An Introduction to Subprocess in Python with Examples

Subprocess in Python By starting new processes, the Python function subprocess is utilized to run extra programs and applications. It makes it possible to run brand-new apps straight from a Programming...

6 minutes read.

__GETITEM__ and __SETITEM__ in Python

These methods are used in assignment operations, unary comparison operations, binary comparison operations and binary operations. These are pre-defined methods that perform many operations on a class instance. Examples like...

3 minutes read.

Python Tkinter Tutorial

What is Tkinter? Tkinter is GUI library of Python. When we integrated Tinkter with Python, it provides an easy and quick way to develop GUI applications. It provides the Tk GUI...

14 minutes read.

How to find square root in python

How to find Square Root of a number In Python Python makes a lot of tasks easier by using different functions, modules, and libraires. There is an inbuilt function in Python...

6 minutes read.

Recursive Multiplication Python

Recursive Multiplication Python In this tutorial, we will learn how to write a Python program to get the multiplication of two numbers using the recursive approach. In the recursive multiplication approach, we...

2 minutes read.