Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5954821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:01:32+00:00 2026-05-22T18:01:32+00:00

How can I compare one element with the next one in a sorted list,

  • 0

How can I compare one element with the next one in a sorted list, and print out their differences. Any help would be appreciated.

Eg: 
lst = [3.18,10.57,14.95,...]
10.57 - 3.18 =  7.39
14.95 - 10.57 = 4.38
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:01:33+00:00Added an answer on May 22, 2026 at 6:01 pm

    If you are manipulating numerical data, consider using numpy

    import numpy as np
    
    lst = [3.18,10.57,14.95]
    arr = np.array(lst)
    
    diff = np.diff(arr)
    
    >>> diff
    array([ 7.39,  4.38])
    

    You can convert it back to list if you have to:

    diff_list = list(diff)
    

    Otherwise you can iterate over it just like you iterate over a list:

    for item in diff: 
        print(item)
    
    7.39
    4.38
    

    EDIT: the five solutions I timed were pretty close to each other, so choose the one that’s easier to read

    t = timeit.Timer("[b - a for a, b in zip(l, l[1:])]", "l = range(int(1e6))")
    print(t.timeit(1))
    >>> 0.523894071579
    
    t = timeit.Timer("list(np.diff(np.array(l)))", "import numpy as np; l = range(int(1e6))")
    print(t.timeit(1))
    >>> 0.484916915894
    
    t = timeit.Timer("diffs = [l[x + 1] - l[x] for x in range(len(l) - 1)]", "l = range(int(1e6))")
    print(t.timeit(1))
    >>> 0.363043069839
    
    t = timeit.Timer("[(x, y, y - x) for (x, y) in itertools.izip(l, it)]", "l = range(int(1e6)); it = iter(l); it.next()")
    print(t.timeit(1))
    >>> 0.54354596138
    
    # pairwise solution
    t = timeit.Timer("a, b = itertools.tee(l); next(b, None); [(x, y) for x, y in itertools.izip(a, b)]", "l = range(int(1e6));")
    print(t.timeit(1))
    >>> 0.477301120758
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can sort a [MVar a] list? using a as the element to compare
How can I compare strings in a case insensitive way in Python? I would
Do you have any experience with T4 and T4 Editor ? Can you compare
In my Java application i need to compare two list's element whether it is
Can i get the source code for a WAMP stack installer somewhere? Any help
I'm implementing accordion-style behavior, where only one element can be displayed at one point.
I'm making a tool, which can compare and list items. I'd like to animate
there are two databases to compare,but I can't connect one database directly. so I
Can any one please describe this sort of code to understand Java closure. public
I trying to compare the styles of elements on my page. But one element

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.