Group List by Values in Python (3 Examples)
In this Python tutorial, we will explain how to group a list by values.
Here you have an overview:
Let’s start right away!
Create Demo Python List
At the start, we will create a demo Python list that we will use in the examples below. Take a look at how to create my_list
.
my_list = [["John", 25], ["Alice", 32], ["Bob", 25], ["Emma", 32], ["Chris", 28]] print(my_list) # [['John', 25], ['Alice', 32], # ['Bob', 25], ['Emma', 32], ['Chris', 28]]
As you can see, my_list
is a list that contains several sublists. Each sublist represents a person’s name and age. Some of the age values are repeated. Now, let’s see some examples of how to group my_list
by the different age values!
Example 1: Group List by Values Using List Comprehension
In this first example, we will use a combination of list comprehension, map(), and lambda() functions to categorize my_list
by values. Take a look at the code below.
values = set(map(lambda x:x[1], my_list)) grouped_values = [[y[0] for y in my_list if y[1]==x] for x in values] print(grouped_values) # [['Alice', 'Emma'], ['John', 'Bob'], ['Chris']]
In this code, values
is a set containing the unique age values extracted from the sublists in my_list
. The map() function is used with a lambda function to iterate over my_list
and extract the second element (age) from each sublist.
The resulting set values
is then used to group the sublists in my_list
by age. The outer list comprehension iterates over each value in the values
list. For each of these values, it constructs a new list using another list comprehension.
The inner list comprehension extracts the first element (name) from the sublists in my_list
where the second element (age) matches the current value.
The result of this operation is a list of lists, with each inner list containing the first elements from the lists in my_list
that have their second element equal to each corresponding value in values
.
Example 2: Group List by Values Using for Loop
This example uses an empty dictionary and a for loop to store the categorized elements in my_list
as follows.
grouped_values = {} for sublist in my_list: key = sublist[1] value = sublist if key not in grouped_values: grouped_values[key] = [] grouped_values[key].append(value) print(grouped_values) # {25: [['John', 25], ['Bob', 25]], # 32: [['Alice', 32], ['Emma', 32]], 28: [['Chris', 28]]}
As shown in the previous output, here we have first created an empty dictionary to store the grouped values. Then, we iterated over the elements in my_list
and extracted the names and ages from each sublist in my_list
.
In the for loop, an if statement checks if the name is already in the dictionary grouped_values
. If not, it’s added as a new key with an empty list as the value. Then, the current value is appended to the list associated with the corresponding key.
Finally, we get a dictionary where the keys are the grouping ages, and the values are the list of names representing the people at that age.
But if you’re looking for a similar output to the one in Example 1, you can variate the code and obtain a similar output, as shown below.
grouped_values = {} for sublist in my_list: key = sublist[1] value = sublist[0] if key not in grouped_values: grouped_values[key] = [] grouped_values[key].append(value) grouped_values = list(grouped_values.values()) print(grouped_values) # [['John', 'Bob'], ['Alice', 'Emma'], ['Chris']]
In the modified code, we use sublist[1]
to group the sublists based on the age value. The value sublist[0]
(name) is appended to the corresponding key in the grouped_values
dictionary.
Then, the grouped_values
variable is updated using the result of converting the dictionary values to a list using the list() function.
Example 3: Group List by Values Using collections Module
In this third example, we will explore the defaultdict class from the collections module to group my_list
by values. First, we will import the collections module using the code below.
import collections
Now, we can use the defaultdict class from this module to group my_list
by values.
grouped_dict = collections.defaultdict(list) for sublist in my_list: grouped_dict[sublist[1]].append(sublist[0]) grouped_values = grouped_dict.values() print(grouped_values) # dict_values([['John', 'Bob'], ['Alice', 'Emma'], ['Chris']])
This first line creates a new defaultdict where each value is a list. If a key is accessed in the dictionary and it doesn’t exist, instead of raising a KeyError, the dictionary automatically creates that key with a default value, in this case, an empty list.
For each sublist in my_list
, it appends the first element sublist[0]
to the list in grouped_dict
that is associated with the key equal to the second element of the sublist sublist[1]
. If the key doesn’t already exist in grouped_dict
, it is automatically created with a default value of an empty list due to the behavior of defaultdict.
Then the values from grouped_dict
are stored in grouped_values
. Note that grouped_values
is a dict_values object, which is a view on the dictionary’s values.
Video, Further Resources & Summary
Do you need more explanations on how to group a list by values in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the related on Statistics Globe:
- Introduction to the Python Programming Language
- Count Duplicates in List in Python
- Group pandas DataFrame by Week in Python
- Group by Two & Multiple Columns of pandas DataFrame in Python
- Quantile by Group in Python
This post has shown how to group a Python list by values. In case you have additional questions, please let me know in the comments section below.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter