I'm having a hard time understanding the difference here, could some one help?

So I'm used to checking for substrings with

>>>"test" in "testing"
True

So why doesn't this work?

>>>[1, 2, 3] in [1, 2, 3, 4, 5]
False

The first case is testing for a series inside another series, so I understand why

>>>[1, 2, 3] in [[1, 2, 3], 4, 5]
True

is true, but I can't get over the fact that strings and arrays use "in" differently. Is there something for arrays that behaves more like "contains" in set theory?

Posted Nov 1, 2012, 6:58 PM UTC

514
Atarius•

in is just sugar for the .__contains__() method. Lists and strings implement that method differently. I can imagine a couple reasons for the difference, the main one being that substring matching is more common than your second example.

7
sastroneOP•

Yeah, that makes sense, I guess my question was more about why they would change the behavior between two concepts that are almost exactly the same.

2
SavinaRojaSavinaRoja•

If you want to work with set theory concepts, you might be interested in Python's built-in types 'set' and 'frozenset'.

http://docs.python.org/2/library/stdtypes.html#set-types-set-frozenset

5
SavinaRojaSavinaRoja•
>>> a, b = set([1,2,3]), set([1,2,3,4])
>>> a.issubset(b)
True
2
NegatedVoid•

I think your fundamental mistake is thinking that strings are arrays of characters.

They implement some similar things, but really, in the python world - strings are strings.

4
sastroneOP•

But strings are an array of characters! Or at least they are in pretty much every single other language...

4
NegatedVoid•

Python is a bit higher level. It has lists and dictionaries and things as language constructs.

Think of this - how about unicode strings? A character in those can take a variable number of bytes.

Sure, in a lot of ways, strings are like arrays, and they might be implemented using arrays (I don't know if they are).. but they're strings.

When you say "a in b" that has a pretty obvious meaning for a string "is this substring in this string", and another pretty obvious meaning for an array "is this element in this array".

4
sastroneOP•

For unicode, you could just have it be an array of unicode code points (which are variable in length). That is how Java and Go do it, since they both follow the idea that unicode is everywhere and shouldn't be treated differently. As for the difference in usage, I found it unintuitive rather than obvious. I guess I expected an operator of all things to behave similarly among similar data structures.

2
ewiethoffproceedest on to 3•

I think of __contains__ for lists, tuples, hashes, sets, etc. as "normal" behavior because they check for a specific element. It's the __contains__ behavior of a string that strikes me as weird. Strings are just weird. (Hmm, it just occurs to me that xrange doesn't support __contains__ at all.)

4
minorDemocritus•

Unicode strings are not encoded, so they don't use variable bytes in the sense I believe your referring to.

I'm not sure how they are actually implemented, but you can think of them as immutable arrays of integers, with each integer representing a code point.

Variable-width stuff happens with Unicode-capable encodings such as UTF8. But encoded strings are just bytes.

1
zahlmanzahlman•

... What are these "array"s of which you speak? I hope you aren't applying that term to lists.

1
NegatedVoid•

Not at all! Read closely :D

I'm suggesting that other languages have arrays and characters.

2
ckaili•

You can think of strings as sort of like an array, but the fact that each element is a character is crucial in allowing something like:

'abc' in 'abcde'

to make sense. Because each element is a character, it only makes sense for this expression to mean substring.

However, for a generic list, each element can, itself, be any object. Therefore, this notation:

[1, 2, 3] in some_list

can really only mean "[1, 2, 3] exists as an element in some_list", since, as you mention in your original post, an element of the list can be itself another list. It can't also mean sublist because then there would be ambiguity and inconsistency. The fact that strings are confined to characters removes that ambiguity.

If you're looking at set theory, consider using built-in sets as someone else mentioned, which supports a subset method. Alternatively, if your lists are elements of single characters or digits, you can turn them into strings first:

list_a = [1, 2, 3, 4]
str_a = ''.join(map(str, list_a))  #"1234"

list_b = [1, 2, 3]
str_b = ''.join(map(str, list_b))  #"123"

str_b in str_a    #True
1