×

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

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

XXhash Python Examples

XXhash Python: xxHash is an extremely rapid hash calculation that operates inside the confines of RAM. Code is incredibly convenient, and hashes (almost nothing/large endian) are same at all levels. Execution of...

4 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 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.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Cross Entropy in Python

Introduction Cross-entropy loss is frequently combined with the softmax function. Determine the total entropy among the distributions or the cross-entropy, which is the difference between two probability distributions. For the purpose...

5 minutes read.

How to declare array in python

How to declare array in python? Arrays are a great way of storing our numeric values into a variable in continuous locations. For declaring an array in C language, we used...

4 minutes read.

Socket Programming in Python

Socket Programming in Python In this tutorial, we will discuss network programming using Python programming language. We will explore all basic concept of network with Python script. Network Services in Python Python has...

9 minutes read.

What is a Script in Python

Have you ever heard that Python is a scripting language? Or when people address a Python program file as a script? Besides programming, script generally means "a written story for...

3 minutes read.

Polymorphism in Python

What is polymorphism and why is it important? Polymorphism's literal definition is the state of occurring in diverse shapes or forms. When it comes to programming, the idea of polymorphism is crucial....

3 minutes read.