#### [SOLVED] Python - Create a new sublist between two elements in an ordered list

By Nate

Given two elements in an ordered list, I want to make a new list consisting of everything between (and including) those elements. The problem comes if the second point comes before the first point in the list. right is my existing sorted list of objects. rt is the beginning boundary and rb is the end boundary. `new_right = right[right.index(rt): right.index(rb) + 1]`

This snippet works except in the case

`right = [a, rb, c, rt, d, e]`

where I get `[rt, d, e]` instead of the wrapped `[rt, d, e, a, rb]`

Is there a way to do this without creating a complicated method? Some other people's problems were solved by list comprehension. Looking into it, I was able to come up with

``````begin = right.index(rt)
end = right.index(rb)
if end > begin:
new_right = right[begin: end + 1]
else:
new_right = [right[i % r_len] for i in range(begin, r_len + end + 1)]
``````

Which looks pretty ugly to me, but it seems to hold up to my tests. Is there a better way to do this?

EDIT: There were two working solutions to this.

``````rbegin = right.index(rt)
rend = right.index(rb)
if rend >= rbegin:
new_right = right[rbegin: rend + 1]
else:
new_right = [right[i % r_len] for i in range(rbegin, r_len + rend + 1)]
``````

Which is obscenely complicated compared to:

``````if rend > rbegin:
new_right = right[rbegin: rend + 1]
else:
new_right = right[rbegin:] + right[:rend + 1]
``````

#### @brotskydotcom 2020-02-14 08:44:31

``````begin = right.index(rt)
end = right.index(rb)
if end > begin:
new_right = right[begin: end + 1]
else:
new_right = right[begin:] + right[:end + 1]
``````

#### @Nate 2020-02-14 08:49:31

This is missing the plus one in ``` right[:end + 1]``` to include that last element. But otherwise, yes.

#### @brotskydotcom 2020-02-14 08:52:20

Thanks, I corrected the off-by-one error.

#### @Dinac23 2020-02-14 08:28:47

Maybe this?

``````new_right = right[begin: end + 1] if end > begin else right[begin:] + right[:end + 1]
``````

#### @Nate 2020-02-14 08:42:13

This worked, thank you. I'll post my monstrosity as well.

#### @schneebuzz 2020-02-14 08:25:23

Would simply flipping the slice match your tests like so:

``````begin = right.index(rt)
end = right.index(rb)
if end > begin:
new_right = right[begin: end + 1]
else:
new_right = right[end: begin + 1]
``````

#### @Nate 2020-02-14 08:43:02

This seems to be one index off. Took me a while to test it.

#### @schneebuzz 2020-02-14 08:44:54

You can correct that by adding or subtracting one accordingly.

### [SOLVED] How can I reverse a list in Python?

• 2010-10-15 06:59:39
• Leo.peis
• 1417425 View
• 992 Score
• Tags:   python list

### [SOLVED] Finding the index of an item given a list containing it in Python

• 2008-10-07 01:39:38
• Eugene M
• 3664312 View
• 3011 Score
• Tags:   python list indexing

### [SOLVED] How do I get the number of elements in a list?

• 2009-11-11 00:30:54
• y2k
• 3205833 View
• 1891 Score
• Tags:   python list

### [SOLVED] How to remove an element from a list by index

• 2009-03-09 18:16:11
• Joan Venge
• 2501565 View
• 1437 Score
• Tags:   python list

### [SOLVED] What's the difference between lists and tuples?

• 2009-03-09 15:41:25
• Lucas Gabriel Sánchez
• 407352 View
• 1000 Score
• Tags:   python list tuples

### [SOLVED] Getting the last element of a list

• 2009-05-30 19:28:53
• Janusz
• 1967218 View
• 1954 Score
• Tags:   python list indexing