By Nate


2020-02-14 08:08:40 8 Comments

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]

3 comments

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

You can add the trailing and leading slices instead of using a comprehension. Using your code structure:

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.

Related Questions

Sponsored Content

34 Answered Questions

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

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

29 Answered Questions

[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
  • 29 Answer
  • Tags:   python list indexing

7 Answered Questions

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

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

15 Answered Questions

[SOLVED] Convert two lists into a dictionary

18 Answered Questions

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

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

18 Answered Questions

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

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

25 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

11 Answered Questions

[SOLVED] Getting the last element of a list

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

26 Answered Questions

[SOLVED] Get difference between two lists

20 Answered Questions

Sponsored Content