How can I subtract tuples in a list?

2024/11/20 11:32:41

Let's say I have a list with tuples in it.

Something like this:

listnum = [(18,12),(12,20)]

Is there a way I can subtract what is in the tuples and make listnum into:

listnum = [6,8]

As you can see It takes the biggest of the numbers in the tuple and subtracts it by the other.

Answer

Use list comprehension:-

>>> listnum = [(18,12),(12,20)]
>>> [(i-j) for i,j in listnum]
[6, -8]
>>> listnum = [(18,12),(12,20),(32,54),(2,43)]
>>> [(i-j) for i,j in listnum]
[6, -8, -22, -41]

And as you asked for bigger number - smaller; use abs() to calculate it.

>>> listnum = [(18,12),(12,20),(32,54),(2,43)]
>>> [abs(i-j) for i ,j in listnum]
[6, 8, 22, 41]
https://en.xdnf.cn/q/118425.html

Related Q&A

Chart barh matplotlib - overlap bars

Im new user of matplotlib and I have a problem with chart barh: overlap bars. When plot the graph, the bars draws overlapped and I havent found the reason. In my opinion the problem is on re-size the …

Python django image upload is not working

Python Django image upload is not working when I am adding data and click on submit button not give a any response and also not upload any data in the database.models.py fisrst of all i add model file.…

Android bluetooth send message working first time only

I need to send string message from Raspberry PI to Android device. I am getting message first time only. After that it does not work at all. I am using PYTHON code in Raspberry PI. After first time, it…

Scraping data from href

I was trying to get the postcodes for DFS, for that i tried getting the href for each shop and then click on it, the next page has shop location from which i can get the postal code, but i am able to g…

Numpy - how to sort an array of value/key pairs in descending order

I was looking at the problem Fastest way to rank items with multiple values and weightings and came up with the following solution, but with two remaining issues:import numpy as np# set up values keys …

How to extract certain under specific condition in pandas? (Sentimental analysis)

The picture is what my dataframe looks like. I have user_name, movie_name and time column. I want to extract only rows that are first day of certain movie. For example, if movie as first date in the ti…

Flask app.run method does not work with WinPython 3.11.1 and next.js application: fetch failed

When using WinPython 3.10.5 I am able to debug my flask & next.js application using the flask debug mode (to enable hot reloads): app.run(debug=True, host=host, port=port)However, when using WinPyt…

Pythonic way to assign global administrator roles for Azure Active Directory

What specifically needs to be changed in the Python 3 code below in order to successfully assign the Global Administrator role for an Azure Active Directory Tenant to a given service principal? We tri…

Pandas calculating age from a date

I really need help with this one. My previous post was very bad and unclear - Im sorry - I wish I could delete but hopefully this one will be better.I need to calculate the age based off of a date (se…

Create new folders within multiple existing folders with python

I am looking for a way to create new folders within multiple existing folders. For example I have folders a,b,c.. etc and I want to create a new folder inside each of these existing folders and name th…