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

Related searches

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]
```

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]
```

Python, Given a list of lists, write a Python program to extract first element of each sublist in the given list of lists. Examples: Input : [[1, 2], [3, 4, 5], [6, 7, 8,� I have a list of lists in the form: list = [[3, 1], [3, 2], [3, 3]] And I want to split it into two lists, one with the x values of each sublist and one with the y values of each sublist. I

Maybe this?

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

Python, GBlog � Puzzles � What's New ? element in a sorted and rotated array � Reversal algorithm for right rotation of an array Approach will be run two nested loop till the length of the given list. all elements on right of the picked elements as ending element of subarray. function to generate all the sub lists. The outer loop picks starting element and inner loop considers all elements on right of the picked elements as ending element of subarray. To get the subarray we can use slicing to get the subarray. Step 1: Run a loop till length of the given list.

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]
```

11. Lists — How to Think Like a Computer Scientist: Learning with , Lists and strings — and other collections that maintain the order of their items There are several ways to create a new list; the simplest is to enclose the elements in square brackets With the slice operator we can update a whole sublist at once: Since strings are immutable, Python optimizes resources by making two� Till now we have seen two different ways to create an empty python list, now let’s discuss the different ways to append elements to the empty list. Create an empty list and append elements using for loop. Suppose we want to create an empty list and then append 10 numbers (0 to 9 ) to it. Let’s see how to do that,

Append vs Extend in Python Lists, Lists are one of the most useful data structures available in Python, or really any Once we have created a list, often times we may need to add new elements to it, whether it be at python append.py [1, 'x', 2, 'y'] This works well when you have a list in which its items are ordered, so you can't just add your element to the � The list elements are ordered and changeable or mutable with different operations of python. Each element of the list is arranged with a sequence whose assigned index starts from zero(0). The elements are comma-separated and can be of different data types. Creating A List in Python. To create a list in Python, you have to place elements of

Lists and Tuples in Python – Real Python, You'll cover the important characteristics of lists and tuples in Python 3. Lists Are Ordered; Lists Can Contain Arbitrary Objects; List Elements Can Be Accessed by Index Conversely, if a is a list, a[:] returns a new object that is a copy of a : > x[1][1] is yet another sublist, so adding one more index accesses its elements:. Python program to print all the common elements of two lists. Python program to Count Even and Odd numbers in a List; Missing even and odd elements from the given arrays in C++; Python Program for Odd-Even Sort / Brick Sort; Python Program for Difference between sums of odd and even digits; C# program to print all the common elements of two

Previous: Write a Python program to check whether a list contains a sublist. Next: Write a Python program using Sieve of Eratosthenes method for computing primes upto a specified number. What is the difficulty level of this exercise?