Question #7092   Submitted by Answiki on 11/09/2022 at 04:35:03 PM UTC

In Python, how to pick a sub list of consecutive elements in a list?

Answer   Submitted by Answiki on 11/09/2022 at 04:36:22 PM UTC

In Python, to pick a random sub sequence of consecutive elements in a given list, the best option is to pick two random values (left and right) and then to extract the sub list with the syntax myList[left:right]. Here is a full example that pick 20 sub sequances:

from random import randint

# List 
myList = [1,2,3,4,5,6,7,8,9,10]

# Left and right indexes are picked randomly
left = randint(0, len(myList)-1)
right = randint(left+1, len(myList))
# extract sub list
sub = myList[left:right]
print (sub)

Here are some examples of output:

[4]
[5, 6, 7, 8]
[6, 7, 8]
[5, 6, 7, 8, 9, 10]
[3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1]
[5]
[8, 9]
[2]
[5, 6, 7, 8]
[10]
[5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7]
[6, 7, 8]
[6, 7]
[6, 7, 8, 9, 10]
[1, 2, 3]
[2, 3, 4, 5, 6, 7, 8]

8 events in history
Answer by Answiki on 11/09/2022 at 04:36:22 PM

In Python, to pick a random sub sequence of consecutive elements in a given list, the best option is to pick two random values (left and right) and then to extract the sub list with the syntax myList[left:right]. Here is a full example that pick 20 sub sequances:

from random import randint

# List 
myList = [1,2,3,4,5,6,7,8,9,10]

# Left and right indexes are picked randomly
left = randint(0, len(myList)-1)
right = randint(left+1, len(myList))
# extract sub list
sub = myList[left:right]
print (sub)

Here are some examples of output:

[4]
[5, 6, 7, 8]
[6, 7, 8]
[5, 6, 7, 8, 9, 10]
[3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1]
[5]
[8, 9]
[2]
[5, 6, 7, 8]
[10]
[5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7]
[6, 7, 8]
[6, 7]
[6, 7, 8, 9, 10]
[1, 2, 3]
[2, 3, 4, 5, 6, 7, 8]

Question by Answiki 11/09/2022 at 04:36:06 PM
In Python, how to pick a random subsequence of consecutive elements in a list?
Question by Answiki 11/09/2022 at 04:35:41 PM
In Python, how to pick a random sub sequence of consecutive elements in a list? replaced by #7096.
Question by Answiki 11/09/2022 at 04:35:30 PM
In Python, how to pick a random sub list of consecutive elements in a list?
Question by Answiki 11/09/2022 at 04:35:16 PM
In Python, how to randomly pick a sub list of consecutive elements in a given list?
Question by Answiki 11/09/2022 at 04:35:03 PM
In Python, how to pick a sub list of consecutive elements in a list? replaced by #7094.
Answer by Answiki on 11/09/2022 at 04:34:50 PM

In Python, to pick a random sub sequence of consecutive elements in a given list, the best option is to pick two random values (left and right) and then to extract the sub list with the syntax myList[left:right]. Here is a full example that pick 20 sub sequances:

from random import randint

# List 
myList = [1,2,3,4,5,6,7,8,9,10]

# Left and right indexes are picked randomly
left = randint(0, len(myList)-1)
right = randint(left+1, len(myList))
# extract sub list
sub = myList[left:right]
print (sub)

Here is some example of outputs:

[4]
[5, 6, 7, 8]
[6, 7, 8]
[5, 6, 7, 8, 9, 10]
[3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1]
[5]
[8, 9]
[2]
[5, 6, 7, 8]
[10]
[5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7]
[6, 7, 8]
[6, 7]
[6, 7, 8, 9, 10]
[1, 2, 3]
[2, 3, 4, 5, 6, 7, 8]

Question by Answiki 11/09/2022 at 04:27:29 PM
In Python, how to pick a random sequence of consecutive elements in a list?
# ID Query URL Count

Icons proudly provided by Friconix.