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.