How to split a list by string

list to string python
'list' object has no attribute 'split'
split list into multiple lists python
python split list at index
python split string by character count
python split string by space
how to split a continuous string in python
python split number from string

I have several large lists of strings. I need to split all of those lists into two lists so that I keep only the 2nd list. For example:

lst = ['This is a list', 'of strings', 'blahblahblah', 'split_here', 'something else', 'we like cake', 'aardvarks']

I want to grab only the strings after 'split_here' so that the new list will be this:

new_lst = ['something else', 'we like cake', 'aardvarks']

I tried this:

new_list = str(lst).split('split_here')[1]

But the new output has a bunch of escape characters (the "\" symbol). I tried replacing them with:

.replace('\\', '')

But that didn't work either.

I'm thinking there has to be a simple way to do this that I'm missing.

You're looking for list operations, not for string operations. We just need to find the position where the separator string appears, and take a slice starting from the next element, like this:

lst = ['This is a list', 'of strings', 'blahblahblah', 'split_here', 'something else', 'we like cake', 'aardvarks']
new_list = lst[lst.index('split_here')+1:]

The above assumes that the separator string is present in the list, otherwise we'll get a ValueError. The result is as expected:

=> ['something else', 'we like cake', 'aardvarks']

Python, Given a list of strings, write a Python program to split the list into sublists based on string length. Examples: Input : ['The', 'art', 'of', 'programming'] Output : [['of'],  Split the string into a list with max 2 items: txt = "apple#banana#cherry#orange". # setting the maxsplit parameter to 1, will return a list with 2 elements! x = txt.split ("#", 1) print(x) Try it Yourself ». String Methods.

Using list#index is probably the cleanest solution.

Still, since you were trying to find a solution with strings and split, you could use:


Note that it only works if you're sure that # never appears in your strings.

Here are the steps shown in the console:

>>> lst = ['This is a list', 'of strings', 'blahblahblah', 'split_here', 'something else', 'we like cake', 'aardvarks']
>>> '#'.join(lst)
'This is a list#of strings#blahblahblah#split_here#something else#we like cake#aardvarks'
>>> '#'.join(lst).split('split_here#')
['This is a list#of strings#blahblahblah#', 'something else#we like cake#aardvarks']
>>> '#'.join(lst).split('split_here#')[-1]
'something else#we like cake#aardvarks'
>>> '#'.join(lst).split('split_here#')[-1].split('#')
['something else', 'we like cake', 'aardvarks']

Python, This can be multiple pairs for split. Let's discuss certain way to solve this particular problem. Method : Using loop + index() + list slicing. This task can be performed  Python 3 Examples How to Split String into List Simple split of string into list. If you want to split any string into a list (of substrings) you can use simply the Python split string by separator. Python split string by comma or any other character use the same method split () with Split

Adding to Oscar answer, You can use itertools.dropwhile() too:

from itertools import dropwhile

lst = ['This is a list', 'of strings', 'blahblahblah', 'split_here', 'something else', 'we like cake', 'aardvarks']
new_list = list(dropwhile(lambda x: x != 'split_here',lst))[1:]

Python String split() Method, The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will  How to split a string, within a list, into individual characters without using loops. Hello everyone, Is there a way to split a string, within a list, without using a

Python 3 Notes: Split and Join, For that, you need a different data type: a list of strings where each string .split() splits mary on whitespce, and the returned result is a list of words in mary. Another way to convert a string to a list is to use the split() method that breaks a string into substrings using whitespaces as delimiters: s = 'hello world' lst = s.split() print(lst) # ['hello', 'world'] You can also modify the delimiter string by passing it into the split() function.

Convert list of string to list of list in Python, Using strip and split. We use these two methods which will first separate out the lists and then convert each element of the list to a string. Code #1 : Using For loop This approach uses for loop to convert each character into a list. Just enclosing the For loop within square brackets [] will split the characters of word into list.

Java - String to ArrayList conversion, The steps for conversion are as follows: 1) First split the string using String split() This method returns a list created based on the elements of the specified array​  The string to split can have a comma or a semicolon delimiter. Using an SQL Server query, you can cut a string compounded of text delimited by commas, semicolons, tabulations, dash, underscore, or even dots.

  • Thank you! That was much simpler than the things I was trying out. :)
  • @RagnarLothbrok Please consider this: