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
?
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.
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.")
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.
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] .
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
andnot in
test for collection membership.x in s
evaluates to true ifx
is a member of the collections
, and false otherwise.x not in s
returns the negation ofx 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 indexi
such thatx == y[i]
is true.For the Unicode and string types,
x in y
is true if and only ifx
is a substring ofy
. An equivalent test isy.find(x) != -1
. Note,x
andy
need not be the same type; consequently,u'ab'
in'abc'
will returnTrue
. Empty strings are always considered to be a substring of any other string, so""
in"abc"
will returnTrue
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With