How does one split a list in python knowing a sublist of it?

Viewed 67

I am looking for a more efficient way (if available) to split a list into two sub-lists. An example of the original list:

full_list = ['t1', 't2', 't3', 't4', 't5', 'v1', 'v2']

NOTE: In general the original list contains an arbitrary amount of elements that are mixed together. The 't1' ... 't5' just serve to indicate elements of the first sub-list:

t_sub_list = ['t1', 't2', 't3', 't4', 't5']

This sub-list is a given.

I would like to most efficiently generate the second sub-list:

v_sub_list = ['v1', 'v2']

The solutions that come to my mind are:

v_sub_list_A = [list_element for list_element in full_list if list_element not in t_sub_list]
v_sub_list_B = list(set(full_list) - set(t_sub_list))

The question I have is - is there any more efficient way of doing this? Or at least any package that would allow to achieve the result in a more code-readable fashion?

3 Answers

As stated, it's even easier than that. Since all of the first list elements are on the left, you need only deal with finding the break point. Use the list length.

full_list = ['t1', 't2', 't3', 't4', 't5', 'v1', 'v2']
t_sub_list = ['t1', 't2', 't3', 't4', 't5']

left_size = len(t_sub_list)
v_sub_list = full_list[left_size:]

This yields the result you described.

you can compare the t_sub_list with all your full_list so it will split the v_sub_list.....

full_list = ['t1', 't2', 't3', 't4', 't5', 'v1', 'v2']
t_sub_list= ['t1', 't2', 't3', 't4', 't5']
v_sub_list=[]

for i in full_list:                 
    if i in t_sub_list:
        pass
    if i not in t_sub_list:
        v_sub_list.append(i)
print(v_sub_list)

the output will be.....

['v1', 'v2']

since t_sub_list it is not always at the beginning of full_list and your elements are unique you can use:

full_list = ['t1', 't2', 't3', 't4', 't5', 'v1', 'v2']
t_sub_list = ['t1', 't2', 't3', 't4', 't5']

start_index = full_list.index(t_sub_list[0])
t_size = len(t_sub_list)

v_sub_list = full_list[0:start_index] + full_list[start_index + t_size:]
# ['v1', 'v2']

you are searching the index for the first item from t_sub_list and then you calculate the size of t_sub_list in order to take all the elements around your t_sub_list


if in your t_sub_list are random numbers form full_list you can use a list comprehension:

t_sub_lis = set(t_sub_lis) # will make search O(1)
v_sub_list = [e for e in full_list if e not in t_sub_list]
Related