Home >Backend Development >Python Tutorial >How to Extract Elements Before a Delimiter in a Python List?
Extracting Elements from a List in Python
Suppose you have a list containing elements separated by a delimiter, such as a tab character. To extract only the elements before the delimiter, you can utilize Python's list comprehension feature.
Specific Problem:
You have a list:
my_list = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
How can you remove the tab character (t) and everything after it to obtain the following result:
['element1', 'element2', 'element3']
Solution:
<code class="python">[i.split('\t', 1)[0] for i in my_list]</code>
Explanation:
The above is the detailed content of How to Extract Elements Before a Delimiter in a Python List?. For more information, please follow other related articles on the PHP Chinese website!