I have a doubt on how to use the split function.
str = 'James;Joseph;Arun;'
str.split(';')
I got the result ['James', 'Joseph', 'Arun', '']
I need the output as ['James', 'Joseph', 'Arun']
What is the best way to do it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To remove all empty strings you can use a list comprehension:
Or the filter/bool trick:
Note that this will also remove empty strings at the start or in the middle of the list, not just at the end.
If you just want to remove the empty string at the end you can use
rstripbefore splitting.