user2403149
user2403149

Reputation:

Returning Specific List Items In Python

I have a list, which contains the names of several text files, like this:

["catfile.txt", "order_2014_11_11_11", "santa.txt", "order_2013_10_20"]    

How can I check AND return these elements of the list, that start with "order_" or any other given sequence?

Upvotes: 0

Views: 60

Answers (5)

Lam
Lam

Reputation: 1

Try this code:

l1 = ["catfile.txt", "order_2014_11_11_11", "santa.txt", "order_2013_10_20"]
l2 = []

for i in l1:
    if i.startswith("order"):
        l2.append(i)

Samples and output

print l1
['catfile.txt', 'order_2014_11_11_11', 'santa.txt', 'order_2013_10_20']

print l2
['order_2014_11_11_11', 'order_2013_10_20']

Upvotes: 0

John1024
John1024

Reputation: 113924

>>> data = ["catfile.txt", "order_2014_11_11_11", "santa.txt", "order_2013_10_20"]    
>>> [ x for x in data if x.startswith('order_')]
['order_2014_11_11_11', 'order_2013_10_20']

Explanation:

A list comprehension typically looks something like:

[somefunction(x) for x in data if some_condition]

In our case, we only want to select items from data, not manipulate them. So, somefunction is not needed and the expression simplifies to:

[x for x in data if some_condition]

In our case, the condition is that the string starts with order_. Python has a handy string method to test just for this. It is called, naturally enough, startswith. So, the final form is:

[ x for x in data if x.startswith('order_')]

If you ever have need for it, there is an analogous string method to test the end of a string. For example:

>>> [ x for x in data if x.endswith('txt')]
['catfile.txt', 'santa.txt']

Upvotes: 4

Parker
Parker

Reputation: 8851

seq = "order_"
results = [item for item in list if item.startswith(seq)]

Upvotes: -2

user2555451
user2555451

Reputation:

You can use a list comprehension to filter the list with str.startswith:

>>> lst = ["catfile.txt", "order_2014_11_11_11", "santa.txt", "order_2013_10_20"]
>>> [x for x in lst if x.startswith('order_')]
['order_2014_11_11_11', 'order_2013_10_20']
>>>

You can even make this an in-place operation by using [:]:

>>> lst = ["catfile.txt", "order_2014_11_11_11", "santa.txt", "order_2013_10_20"]
>>> lst[:] = [x for x in lst if x.startswith('order_')]
>>> lst
['order_2014_11_11_11', 'order_2013_10_20']
>>>

Finally, it should be noted that str.startswith allows you to specify a tuple of prefixes to search for:

>>> lst = ["catfile.txt", "order_2014_11_11_11", "santa.txt", "order_2013_10_20"]
>>> [x for x in lst if x.startswith(('order_', 'cat'))]
['catfile.txt', 'order_2014_11_11_11', 'order_2013_10_20']
>>>

Upvotes: 1

inspectorG4dget
inspectorG4dget

Reputation: 114035

In [92]: L = ["catfile.txt", "order_2014_11_11_11", "santa.txt", "order_2013_10_20"]

In [93]: answer = []

In [94]: for elem in L:
   ....:     if elem.startswith("order_"):
   ....:         answer.append(elem)
   ....:         

In [95]: answer
Out[95]: ['order_2014_11_11_11', 'order_2013_10_20']

As a function:

In [96]: def returnOrders(L):
   ....:     answer = []
   ....:     for elem in L:
   ....:         if elem.startswith("order_"):
   ....:             answer.append(elem)
   ....:     return answer
   ....: 

In [97]: L
Out[97]: ['catfile.txt', 'order_2014_11_11_11', 'santa.txt', 'order_2013_10_20']

In [98]: returnOrders(L)
Out[98]: ['order_2014_11_11_11', 'order_2013_10_20']

More generally:

In [99]: def returnStarters(L, prefix):
   ....:     answer = []
   ....:     for elem in L:
   ....:         if elem.startswith(prefix):
   ....:             answer.append(elem)
   ....:     return answer
   ....: 

In [100]: L
Out[100]: ['catfile.txt', 'order_2014_11_11_11', 'santa.txt', 'order_2013_10_20']

In [101]: returnStarters(L, "order_")
Out[101]: ['order_2014_11_11_11', 'order_2013_10_20']

Upvotes: 0

Related Questions