OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Extract a list item in Python that contains a string, without list comprehension?

  • Thread starter Thread starter Pyderman
  • Start date Start date
P

Pyderman

Guest
Given a list:

Code:
my_list = ['abc', 'def', 'ghi']

I want to extract the element that contains 'ab' (of which there will only ever be one).

Rather than something a little convoluted like:

Code:
[i for i in my_list if 'ab' in i][0]

I'm looking for some way of applying something that does the equivalent of XPath contains(), but for a list.
<p>Given a list:</p>

<pre><code>my_list = ['abc', 'def', 'ghi']
</code></pre>

<p>I want to extract the element that contains 'ab' (of which there will only ever be one).</p>

<p>Rather than something a little convoluted like:</p>

<pre><code>[i for i in my_list if 'ab' in i][0]
</code></pre>

<p>I'm looking for some way of applying something that does the equivalent of XPath <code>contains()</code>, but for a list.</p>
 

Latest posts

Top