Extract items from the list with comes in between another list items
a = [4,8]
b = [1,2,3,4,5,6,7,8,9,0]
I want to extract values from b list which comes in between 4 and 8 values of list a ?
Result should be
b = [5,6,7]
In [8]: a = [4,8]
In [9]: b = [1,2,3,4,5,6,7,8,9,0]
In [10]: [ item for item in b if a[0] < item < a[1]]
Out[10]: [5, 6, 7]
a[0] < item < a[1]Others have posted answers that rely on ordered lists of integers. However, if you are operating on unordered lists where values can be of any type, this answer will work where others do not.
result = b[b.index(a[0]) + 1 : b.index(a[1])]
In[2]: a = [1, "a"]; b = [0.229, "b", 1, [], "c", 5, "5", 2, "a"]
In[3]: b[b.index(a[0]) + 1 : b.index(a[1])]
Out[4]: [[], 'c', 5, '5', 2]
aalways have 2 values only?b = [10,20,25,30]canabe[23, 27]or a will be[1,3]?