Python's in
doesn't work on whole columns:
> dfTrain['name'][22]
'McGowan, Miss. Anna "Annie"'
> "\"" in dfTrain['name'][22]
True
> "\"" in dfTrain['name']
False
How can I check if a character is present in a list of strings?
Dan
11.7k12 gold badges50 silver badges81 bronze badges
asked Apr 21, 2016 at 16:54
3
"\"" in dfTrain['name'][22] is 'McGowan, Miss. Anna "Annie"' which contains "\"
while dfTrain['name'] is a list and you dont have a "\" as element in list
Similar example as yours:
>>> nested_list_example = ["abhishek","ralesh","wr'"]
>>> "wr'" in nested_list_example
True
>>> "'" in nested_list_example
False
>>> "'" in nested_list_example[2]
True
answered Apr 21, 2016 at 16:58
abhinsitabhinsit
3,1544 gold badges20 silver badges25 bronze badges
0
There can be several ways of doing this:
1] One of the things you can do is
"\"" in dfTrain['name'].to_string[]
This returns True
if any of the names in
the df contain a ".
2] The other way could be not dfTrain[dfTrain['name'].str.contains['"']].empty
This is because, I am finding all columns that contain "
. If there are no columns that contain "
it means the dataframe returned will be empty. If the dataframe returned is empty[True] then none of the columns contain a "
for which you want the output 'False' [hence the 'not' statement']
answered Apr 22, 2016 at 14:49
CoderBCCoderBC
1,1822 gold badges11 silver badges28 bronze badges
you can join[]
elements in each row into one string and use
contains[]
in order to check whether it contains "
:
In [11]: df
Out[11]:
name
0 [test1, test2]
1 [another test]
2 [yet, another test]
3 [McGowan, Miss. Anna "Annie", aaa, bbb]
In [12]: df['name'].str.join[''].str.contains['"']
Out[12]:
0 False
1 False
2 False
3 True
Name: name, dtype: bool
answered Apr 21, 2016 at 17:41
We can use Python in
operator to check if a string is present in the list or not. There is also a not in
operator to check if a string is not present in the list.
l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']
# string in the list
if 'A' in l1:
print['A is present in the list']
# string not in the list
if 'X' not in l1:
print['X is not present in the list']
Output:
A is present in the list
X is not present in the list
Recommended Reading: Python f-strings Let’s look at another example where we will ask the user to enter the string to check in the list.
l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']
s = input['Please enter a character A-Z:\n']
if s in l1:
print[f'{s} is present in the list']
else:
print[f'{s} is not present in the list']
Output:
Please enter a character A-Z:
A
A is present in the list
Python Find String in List using count[]
We can also use count[] function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list.
l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']
s = 'A'
count = l1.count[s]
if count > 0:
print[f'{s} is present in the list for {count} times.']
Finding all indexes of a string in the list
There is no built-in function to get the list of all the indexes of a string in the list. Here is a simple program to get the list of all the indexes where the string is present in the list.
l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']
s = 'A'
matched_indexes = []
i = 0
length = len[l1]
while i < length:
if s == l1[i]:
matched_indexes.append[i]
i += 1
print[f'{s} is present in {l1} at indexes {matched_indexes}']
Output: A is present in ['A', 'B', 'C', 'D', 'A', 'A', 'C'] at indexes [0, 4, 5]
You can checkout complete python script and more Python examples from our GitHub Repository.