Python Forum
Subtracting Elements of a Sublist in Python 3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subtracting Elements of a Sublist in Python 3
#4
note that your expected output is incorrect and not consistent with your attempt at code
elements with index 0 and 1 are calculated as 0-1 and 2-4, while element with index 2 is calculated as 7-8
in your attempt you do int(sub_list[1])-int(sub_list[0]), so basically if that's correct the output is [1, 2, -1]
>>> spam = [[0, 1], [2,4], [8,7]]
>>> [n2-n1 for n1, n2 in spam]
[1, 2, -1]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Subtracting Elements of a Sublist in Python 3 - by buran - Dec-31-2020, 03:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Repeating elements when appending in iteration in Python 3.6 miguelsantana 2 3,476 Dec-17-2017, 01:22 AM
Last Post: Terafy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020