Python check for character in list of strings

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.

How do you check if a character is present in a list in Python?

We can use the in-built python List method, count[], to check if the passed element exists in the List. If the passed element exists in the List, the count[] method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.

How do you check if a character is in a string?

Use the String. includes[] method to check if a string contains a character, e.g. if [str. includes[char]] {} . The include[] method will return true if the string contains the provided character, otherwise false is returned.

How do you find an element in a list in a list Python?

To find an element in the list, use the Python list index[] method, The index[] is an inbuilt Python method that searches for an item in the list and returns its index. The index[] method finds the given element in the list and returns its position.

How do you check if a string has a word from a list in Python?

Use the any[] function to check if a string contains an element from a list, e.g. if any[substring in my_str for substring in my_list]: . The any[] function will return True if the string contains at least one element from the list and False otherwise.

Chủ Đề