Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I have a list

list={3,7,4,5,1}

how to get the List of Intervals /Ranges

result = {{1,3},{3,4},{4,5},{5,7}}
share|improve this question
up vote 18 down vote accepted

Maybe

Partition[Sort@list, 2, 1]

giving

{{1, 3}, {3, 4}, {4, 5}, {5, 7}}

share|improve this answer
    
Beat me to it - 7 secs! Deleting mine, and +1. – Leonid Shifrin Jan 21 '12 at 9:30

Tom's answer is canonical, but you could also use:

With[{s = Sort[list]}, Transpose[{Most[s], Rest[s]}]]

which seems to be marginally faster for large lists. (In which case most of the time is spent sorting.)

share|improve this answer
2  
Alternatively: Composition[Transpose, Through, {Most, Rest}, Sort][list]. – J. M. Jan 22 '12 at 4:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.