How to split a Python list of strings based on condition?

In this short tutorial we will learn how to divide a Python list objects to multiple sublists.

Example list object

All examples in this tutorial will be based on the following programming languages list :

all_lang_lst = ['R', 'Julia', 'Python', 'Haskell', 'Java', 'Javascript']

Divide a list based with delimiter

In this simple case, the after being split, the elements will be written to a string delimited by commas followed by a space:

', '.join (all_lang_lst)

The output will be:

'R, Julia, Python, Haskell, Java, Javascript'

Split a list by condition

We might want to split a list into multiple sub lists based on a simple condition.

data_lang = ['R', 'Julia', 'Python']
other_lang = ['Java', 'Javascript', 'Haskell']
data_lst = []
other_lst = []

# divide the list values using comprehensions
data_lst = [x for x in all_lang_lst if x in data_lang]
other_lst = [x for x in all_lang_lst if x in other_lang]

#print the lists
print ("The data science related languages are:" + str(data_lst))
print ("The other languages are: " + str(other_lst))

Here’s the output:

The data science related languages are:['R', 'Julia', 'Python']
The other languages are: ['Haskell', 'Java', 'Javascript']

Note: as shown above we can use the join() function to convert any list to a string.

Split Python lists by substring

In a similar fashion we can divide the list based on whether its element contains a specific sub string. We’ll start by defining a function:

def splt_by_substrng (my_lst, my_str):
    subs_lst = []
    subs_lst_other =[]
    for e in my_lst:
        if my_str in e:
    return subs_lst, subs_lst_other

We’ll now call the function that we have just defined. In our case we would like to split programming languages containing the substring ‘ja’ into a separated list. The function will return two lists.

print("Here are the split lists: " + str(splt_by_substrng(all_lang_lst , "Ja")))

The result will be:

Here are the split lists: (['Java', 'Javascript'], ['R', 'Julia', 'Python', 'Haskell'])

Find the difference between two lists

Another way to split lists is simply by subtracting. We can convert our lists to Python sets and simply subtract them:

other_lst= set(all_lang_lst)- set(data_lang)