Module 1: Advanced Python List Methods and Techniques
Module 1: Advanced Python List Methods and Techniques
In other languages, lists are like dynamically scaled arrays (vectors in C++ and
ArrayLists in Java). Lists are a handy tool in Python because they don't always
have to be homogeneous. Data types such as Strings, Integers, and Objects can
all be found in a single list. Lists can be changed even after they are created
because they are mutable.
One of Python's most useful features is the list. They are incredibly adaptable
due to their many hidden tricks.
Organizing a list
Using the sort() function and the following syntax, we can arrange a list either
in ascending or descending order:
In ascending order:
list.sort()
In descending order:
list.sort(True for reverse)
Example:
Python3
# sorting a list using sort() function
Course Module 1
Course Title: MSCS 7101- ADVANCE DATA STRUCTURE 2
PRELIMS: LISTS METHODS AND TECHNIQUES
Output:
[2, 5, 10, 24, 90]
[90, 24, 10, 5, 2]
To temporarily change the order of the list use the sorted() function with the
syntax:
list.sorted()
Python3
# temporary sorting using sorted() method
# ascending order
print(sorted(my_list))
# descending order
my_list_2 = sorted(my_list)
Course Module 1
Course Title: MSCS 7101- ADVANCE DATA STRUCTURE 3
PRELIMS: LISTS METHODS AND TECHNIQUES
print(my_list)
Output:
[2, 5, 10, 24, 90]
[5, 2, 90, 24, 10]
Reversing a list
To reverse the order of a list we use the reverse() function. Its syntax is:
list.reverse()
Example:
Python3
# Reverse a list using reverse()
# reverse
my_list.reverse()
print(my_list)
Output:
[10, 24, 90, 2, 5]
Or we could apply list comprehension to reverse a list:
list = list[::-1]
Example:
Python3
# reverse using list comprehension
my_list = [5, 2, 90, 24, 10]
# reverse
Course Module 1
Course Title: MSCS 7101- ADVANCE DATA STRUCTURE 4
PRELIMS: LISTS METHODS AND TECHNIQUES
print(my_list[::-1])
Output:
[10, 24, 90, 2, 5]
Eliminating duplicates
Python dictionaries don't have duplicate keys. To turn our list into a dictionary
using list elements as keys, we utilize the dict.fromkeys() function. The
dictionary is then converted back to a list.
Example:
Python3
# removing duplicates from a list using dictionaries
Course Module 1
Course Title: MSCS 7101- ADVANCE DATA STRUCTURE 5
PRELIMS: LISTS METHODS AND TECHNIQUES
Output:
[5, 2, 90, 24, 10, 34]
['a', 'b', 'c', 'd', 'e']
Example:
Python3
# filtering with the help of filter() function
# creating a filter function filter all the values less than 20
# filter function
def my_filter(n):
if n > 20:
return n
# driver code
if __name__ == "__main__":
my_list = [5, 2, 90, 24, 10, 2, 95, 36]
my_filtered_list = list(filter(my_filter, my_list))
print(my_filtered_list)
Output:
[90, 24, 95, 36]
Course Module 1
Course Title: MSCS 7101- ADVANCE DATA STRUCTURE 6
PRELIMS: LISTS METHODS AND TECHNIQUES
We can also filter using list comprehension. It is a much easier and elegant way
to filter lists, below is the syntax:
My_list = [item for item in my_list if (condition)]
Example:
Python3
# filtering with the help of list comprehension
my_list = [5, 2, 90, 24, 10, 2, 95, 36]
Output:
[90, 24, 95, 36]
My_list = list(map(function,iterable))
Example:
Python3
# using map() function to modify the text
def squaring(n):
return n**2
# driver code
if __name__ == "__main__":
Course Module 1
Course Title: MSCS 7101- ADVANCE DATA STRUCTURE 7
PRELIMS: LISTS METHODS AND TECHNIQUES
Output:
[25, 4, 8100, 576, 100, 4, 9025, 1296]
A much cleaner approach is using list comprehension.
Example:
Python3
# the same result can be obtained by a much pythonic approach
# i.e., by using list comprehension
my_list = [5, 2, 90, 24, 10, 2, 95, 36]
Output:
[25, 4, 8100, 576, 100, 4, 9025, 1296]
Course Module 1
Course Title: MSCS 7101- ADVANCE DATA STRUCTURE 8
PRELIMS: LISTS METHODS AND TECHNIQUES
# combined
my_combined_list = list(zip(my_list_1, my_list_2))
print(my_combined_list)
Output:
[(5, 6), (2, 3), (90, 91), (24, 25), (10, 12)]
Most_frequent_value =max(set(my_list),key=mylist.count)
Example :
Python3
# to find the most frequent element from the list
my_list = ['a', 'a', 'a', 'b', 'c', 'd', 'd', 'e']
Output:
The most common element is: a
Checking for membership in a list
To check whether an item exists in a list, we use the in statement.
Example:
Python3
Course Module 1
Course Title: MSCS 7101- ADVANCE DATA STRUCTURE 9
PRELIMS: LISTS METHODS AND TECHNIQUES
Output:
True
False
Course Module 1
Course Title: MSCS 7101- ADVANCE DATA STRUCTURE 10
PRELIMS: LISTS METHODS AND TECHNIQUES
print(my_list)
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
References:
Geeks for Geeks. 2023. Advanced Python List Methods and Techniques
Retrieved from https://www.geeksforgeeks.org/advanced-python-list-methods-
and-techniques/
Course Module 1