ADVERTISEMENT
ADVERTISEMENT

Python program to print all group tuples by Kth index element

Here, we have a list of tuples and an element K. We need to create a program that will group all tuples with the same Kth index element and print it.
Submitted by Shivang Yadav, on September 28, 2021

Python programming language allows its users to work on data structures and manipulate them based on some given condition. In this program, we have a list of tuples and an element K. We need to find all the group tuples that have the same Kth element index and print the list that has all the groups.

Input:
myList = [(4, 1), (6, 5), (3, 1), (6, 6), (1, 1), (5,9), (2, 5)], K = 0

Output:
[((4, 1), (3, 1), (1, 1)), ((6, 5), (2, 5)), ((6,6)), ((5, 9)) ]

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered set of values enclosed in square brackets []

list = [3 ,1,  5, 7]

List of tuples is a list whose each element is a tuple.

tupList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]

Now, let's get back to our topic where we will pair combinations of tuples.

Grouping tuples by Kth Index Element

We will be grouping all the kth index elements on the list based on the kth index element of the list. In Python, we have multiple methods or ways that can be used to do the task for us.

Method 1:

One method to solve the problem is by grouping the elements of tuples of the list based on the given index k using the groupby() method. Additionally, we will be using the itemgetter and generator expression.

# Python program to print all group tuples 
# by Kth Index Element

from operator import itemgetter
from itertools import groupby

# Creating and printing the list of tuples
myTupleList = [(4, 1), (6, 5), (3, 1), (6, 6), (1, 1), (5,9), (2, 5)]
print("Elements of list of tuples are " + str(myTupleList))
K = 0

# Grouping tuples by kth index Element 
myTupleList.sort()
groupTupleList = list(tuple(sub) for idx, sub in groupby(myTupleList, key = itemgetter(K)))

# printing resultant list
print("List of grouped tuples is " + str(groupTupleList))

Output:

Elements of list of tuples are [(4, 1), (6, 5), (3, 1), (6, 6), (1, 1), (5, 9), (2, 5)]
List of grouped tuples is [((1, 1),), ((2, 5),), ((3, 1),), ((4, 1),), ((5, 9),), ((6, 5), (6, 6))]

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.