0

I want to extract job titles that consist of either "back-end", "back end" or "backend" from json data of a website. I managed to do so with the following code:

if "back-end" in jobtitle.lower():
            print(jobtitle)
if "back end" in jobtitle.lower():
            print(jobtitle)
if "backend" in jobtitle.lower():
            print(jobtitle)
else:
            continue

The example output is as below:

Software Back-End Developer
(Senior) Back-end PHP Developer
Backend Developer (m/w/d)
Back End Developer
Front-End / Back-End Developer (m/w/d)

How do I make it more concise?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
SPW
  • 35
  • 5

2 Answers2

4

Do you have to use if ... in? Another solution would be to use regular expressions

back[\-\s]?end Try it here

Explanation:

  • back: Match "back"
  • [\-\s]: Any of these characters: - or <whitespace>
  • ?: Zero or one of the previous
  • end: Match "end"

Run it in Python like so:

rexp = re.compile(r"back[\s\-]?end", re.IGNORECASE)
if re.search(rexp, jobtitle):
    print(jobtitle)
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
2

Using regular-expression here would be optimal, because it's quick and it will solve it in one row.

However, if you want to use an if ... in statement for each option, then you can use any() to compare them in the same statement:

x = """Software Back-End Developer
(Senior) Back-end PHP Developer
Backend Developer (m/w/d)
Back End Developer
Front-End / Back-End Developer (m/w/d)""".splitlines()

for row in x:
  if any(i in row.lower() for i in ["backend", "back end", "back-end"]):
    print(row)
Hampus Larsson
  • 3,050
  • 2
  • 14
  • 20