Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String vs list membership check

So i'm wondering why this:

'alpha' in 'alphanumeric'

is True, but

list('alpha') in list('alphanumeric')

is False.

Why does x in s succeed when x is a substring of s, but x in l doesn't when x is a sublist of l?

like image 413
Liviu Avatar asked May 11 '15 19:05

Liviu


People also ask

How do I check my string membership?

A Python Program to Know Whether a String Exists in the Main String or Not. Example: str1 = input('Please enter first string: ') str2 = input('Please enter second string: ') if str2 in str1: print(str2+' found in the first string. ') else: print(str2+' not found in the first string.

How do I check if a variable is a list or a string?

Check if Variable is a List with type() Now, to alter code flow programatically, based on the results of this function: a_list = [1, 2, 3, 4, 5] # Checks if the variable "a_list" is a list if type(a_list) == list: print("Variable is a list.") else: print("Variable is not a list.")

How do I check if a string contains only a list in Python?

Use the all() function to check if a list contains only strings, e.g. if all(isinstance(item, str) for item in my_list): . The all() function will return True if the list contains only strings and False otherwise.

How do you check the membership of an element in a list Python?

You can check membership of an individual object in a list using the “ in ” keyword operator. For example, the expression 42 in [1, 2, 42] returns True because the integer 42 exists in the list [1, 2, 42] .


2 Answers

When you use list function with any iterable, a new list object will be created with all the elements from the iterable as individual elements in the list.

In your case, strings are valid Python iterables, so

>>> list('alpha')
['a', 'l', 'p', 'h', 'a']
>>> list('alphanumeric')
['a', 'l', 'p', 'h', 'a', 'n', 'u', 'm', 'e', 'r', 'i', 'c']

So, you are effectively checking if one list is a sublist of another list.

In Python only Strings have the in operator to check if one string is part of another string. For all other collections, you can only use individual members. Quoting the documentation,

The operators in and not in test for collection membership. x in s evaluates to true if x is a member of the collection s, and false otherwise. x not in s returns the negation of x in s. The collection membership test has traditionally been bound to sequences; an object is a member of a collection if the collection is a sequence and contains an element equal to that object. However, it make sense for many other object types to support membership tests without being a sequence. In particular, dictionaries (for keys) and sets support membership testing.

For the list and tuple types, x in y is true if and only if there exists an index i such that x == y[i] is true.

For the Unicode and string types, x in y is true if and only if x is a substring of y. An equivalent test is y.find(x) != -1. Note, x and y need not be the same type; consequently, u'ab' in 'abc' will return True. Empty strings are always considered to be a substring of any other string, so "" in "abc" will return True.

like image 54
thefourtheye Avatar answered Sep 28 '22 04:09

thefourtheye


For the second one you are asking if

['a', 'l', 'p', 'h', 'a'] in ['a', 'l', 'p', 'h', 'a', 'n', 'u', 'm', 'e', 'r', 'i', 'c']

and there is no sub-list in the second list only characters.

['a', 'l', 'p', 'h', 'a'] in [['a', 'l', 'p', 'h', 'a'], ['b', 'e', 't', 'a']]

would be true

like image 25
John Avatar answered Sep 28 '22 06:09

John