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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With