Write a python program to order tuples using an external list given during run time

Python program to Order Tuples using external List

PythonServer Side ProgrammingProgramming

When it is required to order the tuples using an external list, the list comprehension, and ‘dict’ method can be used.

Below is the demonstration of the same −

Python – Order Tuples by List

Sometimes, while working with Python tuples, we can have a problem in which we need to perform ordering of all the tuples keys using external list. This problem can have application in data domains such as Data Science. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [[‘Gfg’, 10], [‘best’, 3], [‘CS’, 8], [‘Geeks’, 7]], ord_list = [‘Geeks’, ‘best’, ‘CS’, ‘Gfg’]
Output : [[‘Geeks’, 7], [‘best’, 3], [‘CS’, 8], [‘Gfg’, 10]]

Input : test_list = [[‘best’, 3], [‘CS’, 8], [‘Geeks’, 7]], ord_list = [‘Geeks’, ‘best’, ‘CS’]
Output : [[‘Geeks’, 7], [‘best’, 3], [‘CS’, 8]]

Method #1 : Using dict[] + list comprehension
The combination of above functions can be used to solve this problem. In this, we perform this task by converting tuple list to dictionaries, and as a second step use list comprehension to iterate through list and map the dictionary keys with values.




# Python3 code to demonstrate working of
# Order Tuples by List
# Using dict[] + list comprehension
# initializing list
test_list = [['Gfg', 3], ['best', 9], ['CS', 10], ['Geeks', 2]]
# printing original list
print["The original list is : " + str[test_list]]
# initializing order list
ord_list = ['Geeks', 'best', 'CS', 'Gfg']
# Order Tuples by List
# Using dict[] + list comprehension
temp = dict[test_list]
res = [[key, temp[key]] for key in ord_list]
# printing result
print["The ordered tuple list : " + str[res]]
Output :

The original list is : [['Gfg', 3], ['best', 9], ['CS', 10], ['Geeks', 2]] The ordered tuple list : [['Geeks', 2], ['best', 9], ['CS', 10], ['Gfg', 3]]

Method #2 : Using setdefault[] + sorted[] + lambda
The combination of above functions can be used to solve this problem. In this, we perform task of mapping elements to indices and creating a lookup using setdefault. And, as a second step, using sorted to sort list using lookup dictionary value list.




# Python3 code to demonstrate working of
# Order Tuples by List
# Using setdefault[] + sorted[] + lambda
# initializing list
test_list = [['Gfg', 3], ['best', 9], ['CS', 10], ['Geeks', 2]]
# printing original list
print["The original list is : " + str[test_list]]
# initializing order list
ord_list = ['Geeks', 'best', 'CS', 'Gfg']
# Order Tuples by List
# Using setdefault[] + sorted[] + lambda
temp = dict[]
for key, ele in enumerate[ord_list]:
temp.setdefault[ele, []].append[key]
res = sorted[test_list, key = lambda ele: temp[ele[0]].pop[]]
# printing result
print["The ordered tuple list : " + str[res]]
Output : The original list is : [['Gfg', 3], ['best', 9], ['CS', 10], ['Geeks', 2]] The ordered tuple list : [['Geeks', 2], ['best', 9], ['CS', 10], ['Gfg', 3]]




Article Tags :
Python
Python Programs
Python List-of-Tuples
Python tuple-programs
Read Full Article

Introduction

While dealing with data science domain, we may come across the scenario where we have to order the elements based on the external input. The task is to order the input tuple based on the external element provided.

Program

Approach 1

ip_list = [ ['alpha', 3 ], ['beta', 9], ['gamma', 1] ] ex_list = ['gamma', 'beta', 'alpha'] print["The given list : "+str[ip_list]] #Using dict[] t_list = dict[ip_list] output = [[key, t_list[key]] for key in ex_list] print["The ordered tuple list: "+str[output]]

Output:

Approach 2

ip_list = [ ['alpha', 3 ], ['beta', 9], ['gamma', 1] ] ex_list = ['gamma', 'beta', 'alpha'] print["The given list : "+str[ip_list]] # Using setdefault[], sorted[] and lambda[] x = dict[] for key, ele in enumerate[ex_list]: x.setdefault[ele, []].append[key] output = sorted[ip_list, key = lambda ele: x[ele[0]].pop[]] print["The ordered tuple list: "+str[output]]

Output:

Lists and Tuples in Python

by John Sturtz basics python
Mark as Completed
Tweet Share Email

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Lists and Tuples in Python

Lists and tuples are arguably Python’s most versatile, useful data types. You will find them in virtually every nontrivial Python program.

Here’s what you’ll learn in this tutorial: You’ll cover the important characteristics of lists and tuples. You’ll learn how to define them and how to manipulate them. When you’re finished, you should have a good feel for when and how to use these object types in a Python program.

Take the Quiz: Test your knowledge with our interactive “Python Lists and Tuples” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

5. Data Structures¶

This chapter describes some things you’ve learned about already in more detail, and adds some new things as well.

Python List

In this tutorial, we'll learn everything about Python lists: creating lists, changing list elements, removing elements, and other list operations with the help of examples.

Python Tuples

❮ Previous Next ❯
mytuple = ["apple", "banana", "cherry"]

Video liên quan

Bài mới nhất

Chủ Đề