1

I am using difflib.Differ() on two lists.

The way Differ works, it appends a + if a line is unique to sequence 2 and a - if a line is unique to sequence 1. It appends this right at the beginning of the sequence.

I want to search for sequences in my list that begin with - or + but only if the string begins with this character as the majority of my sequences have these characters in other places within the string.

In the code snippet below, diff_list is the list. I want it to check for a + or - in the very first place in the string value of every sequence in this list:

for x in diff_list:
    if "+" or "-" in x[0]:
        print x

This output seems to print all of the lines even those that don't begin with - or +

Michael May
  • 113
  • 5
  • 1
    `"+" or "-" in x[0]:` is always True, since `"+"` is always True, you wanted - `"+" in x[0] or "-" in x[0]:` or if `x` is string, - `x[0] in ['+','-']` . – Anand S Kumar Sep 18 '15 at 14:38
  • Thank you. How do I mark this as the correct answer? – Michael May Sep 18 '15 at 14:40
  • You cannot mark comments as correct answer. You can answer the question and mark it as correct if you want to. – Anand S Kumar Sep 18 '15 at 14:41
  • possible duplicate of [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – Łukasz Rogalski Sep 18 '15 at 14:41

1 Answers1

4

Did you try startswith?

s = '+asdf'  # sample data

if s.startswith('+') or s.startswith('-'):
    pass  # do work here

Docs: https://docs.python.org/3.4/library/stdtypes.html#str.startswith

moorecm
  • 349
  • 2
  • 5