AIR
AIR

Reputation: 825

How to exclude one word from python list?

I am trying to work on Python Regex. I have a list as follows:

['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF','ABC','DEF']

I need to select all values start with MYTAB, excluding MYTAB-2. Result should be :

['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-PERF']

What should be the regex syntax for it?

Upvotes: 0

Views: 611

Answers (6)

sreisman
sreisman

Reputation: 668

Try using pyregex to find your regex searches. Here is a a link to a working regex for your question

In this case it uses MYTAB-[^2] as the regex pattern.

Upvotes: 0

Mad Physicist
Mad Physicist

Reputation: 114300

You have a number of options for filtering the list. The basic answer to your question is 'MYTAB(?!-2)' if you are using re.match, which matches the beginning of the input.

import re
expr = re.compile('MYTAB(?!-2)')
rawList = ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF','ABC','DEF']
filteredList = [x for x in rawList if expr.match(x)]

However, there is a simpler way to do this since you are looking at a prefix:

filteredList = [x for x in rawList if x.startswith('MYTAB') and x not x.startswith('MYTAB-2')]

If you are not OK with list comprehensions for any reason, you may want to use the builtin filter function:

filteredList = list(filter(expr.match, rawList))

or even

filteredList = list(filter(lambda x: x.startswith('MYTAB') and not x.startswith('MYTAB-2'), rawList))

Additionally, if you do not wish to keep a reference to your precompiled expression around (e.g., trading efficiency for brevity), you can use re.match instead of re.compile.match:

import re
rawList = ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF','ABC','DEF']
filteredList = [x for x in rawList if re.match(x)]

Upvotes: 1

dot.Py
dot.Py

Reputation: 5157

You can use this piece of code.

  1. It will loop through the initial list l1 and if the item startswith("MYTAB"), it'll be added to list l2
  2. It will find the index of MYTAB-2 inside the l2 list
  3. Remove MYTAB-2

Code:

l1 = ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF','ABC','DEF']
l2 = []

for item in l1:
    #add item to l2 if startswith MYTAB
    if item.startswith('MYTAB'):
        l2.append(item)

#getting index of 'MYTAB-2'
index = l2.index('MYTAB-2')

#removing MYTAB-B
del(l2[index])

#printing l2
l2

Output:

['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-PERF']

Upvotes: 2

Eduardo Cuesta
Eduardo Cuesta

Reputation: 149

>>> import re
>>> list_ = ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF', 'ABC', 'DEF']
>>> filtered_list = filter(lambda str_: re.search(r'^MYTAB(?!-2)', str_), list_)
>>> filtered_list
['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-PERF']

Upvotes: 1

Moses Koledoye
Moses Koledoye

Reputation: 78556

Use a filter with re on a list comprehension:

import re

l = ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF','ABC','DEF']
l = [i  for i in l if re.match(r'MYTAB-[^2]', i)]
print(l)
# ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-PERF']

Upvotes: 1

Eli Sadoff
Eli Sadoff

Reputation: 7308

You can use the regex /MYTAB-[^2]+/ for this. The syntax [^2] means all except for 2.

Upvotes: 0

Related Questions