
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Group By Matching Second Tuple Value in List of Tuples in Python
In this tutorial, we are going to write a program that groups all the tuples from a list that have same element as a second element. Let's see an example to understand it clearly.
Input
[('Python', 'tutorialspoints'), ('Management', 'other'), ('Django', 'tutorialspoints'), ('React', 'tutorialspoints'), ('Social', 'other'), ('Business', 'other')]
Output
{'tutorialspoint': [('Python', 'tutorialspoints'), ('Django', 'tutorialspoints'), ('React', 'tutorialspoints')], 'other’: [('Management', 'other'), ('Social', 'other'), ('Business', 'other')]}
We have to group the tuples from the list. Let's see the steps to solve the problem.
- Initiate a list with required tuples.
- Create an empty dictionary.
- Iterate through the list of tuples.
- Check if the second element of the tuple is already present in the dictionary or not.
- If it already present, then append the current tuple to its list.
- Else initialize the key with a list with the current tuple.
- At the end, you will get a dictionary with the required modifications.
Example
# initializing the list with tuples tuples = [('Python', 'tutorialspoints'), ('Management', 'other'), ('Django', 't ialspoints'), ('React', 'tutorialspoints'), ('Social', 'other'), ('Business', 'othe r')] # empty dict result = {} # iterating over the list of tuples for tup in tuples: # checking the tuple element in the dict if tup[1] in result: # add the current tuple to dict result[tup[1]].append(tup) else: # initiate the key with list result[tup[1]] = [tup] # priting the result print(result)
Output
If you run the above code, then you will get the following result.
{'tutorialspoints': [('Python', 'tutorialspoints'), ('Django', 'tutorialspoints ('React', 'tutorialspoints')], 'other': [('Management', 'other'), ('Social', 'other '), ('Business', 'other')]}
We skip the if condition in the above program using defaultdict. Let's solve it using the defaultdict.
Example
# importing defaultdict from collections from collections import defaultdict # initializing the list with tuples tuples = [('Python', 'tutorialspoints'), ('Management', 'other'), ('Django', 't ialspoints'), ('React', 'tutorialspoints'), ('Social', 'other'), ('Business', 'othe r')] # empty dict with defaultdict result = defaultdict(list) # iterating over the list of tuples for tup in tuples: result[tup[1]].append(tup) # priting the result print(dict(result))
Output
If you run the above code, then you will get the following result.
{'tutorialspoints': [('Python', 'tutorialspoints'), ('Django', 'tutorialspoints ('React', 'tutorialspoints')], 'other': [('Management', 'other'), ('Social', 'other '), ('Business', 'other')]}
Conclusion
You can solve it different in ways as you like. We have seen two ways here. If you have any doubts in the tutorial, mention them in the comment section.
Advertisements