>>> ['a', 'b'].index('b')
1

If the list is already sorted, you can of course do better than linear search.

Answer from AndiDog on Stack Overflow
Discussions

python - Finding specific characters within a list - Stack Overflow
The goal is to make a list from the user's paragraph and iterating so that I can count how many words contain special letters "j,x,q,z". Example input: In a hole in the ground, there lived a hobb... More on stackoverflow.com
🌐 stackoverflow.com
Finding characters in a list python - Stack Overflow
def find_friend(filename, name): lists = open(filename, 'Ur') name1 = set(lists) for row in lists: if name in name1: print row else: print 'frien... More on stackoverflow.com
🌐 stackoverflow.com
python - Search for a char in list of lists - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... I have a list of lists, and I want to return those ... More on stackoverflow.com
🌐 stackoverflow.com
November 12, 2021
python - Searching for a character or string of characters in a list (like the find function on websites) - Stack Overflow
My first post here. I'd like to create a search function, searching a list for any raw_input entered. So far, I've been able to call a path on computer and append each item to a list. I know I can ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › how to check if a string contains a character in a list
r/learnpython on Reddit: How to check if a string contains a character in a list
February 19, 2022 -

I'm really new to python and i'm having some trouble figuring out how to check if a list contains a certain character.

For example:
list = ['a1', 'a2', 'b1', 'b2', 'a3', 'a5', 'c3']

how would I check to see what elements contain the string 'a'? sorry if i didn't word my question properly i'm still very new to coding, but any help would be appreciated.

Top answer
1 of 6
4

Maybe this could be an opportunity to introduce you to some python features:

from typing import List


def rare_char(sentence: str, rare_chars: List[str]=["j", "x", "q", "z"]) -> List[str]:
    return [word for word in sentence.split() if 
            any(char in word for char in rare_chars)]


def cool_para(sentence: str) -> str:
    return f"{len(rare_char(sentence))} word(s) with rare characters"

This answer uses:

  1. typing, which can be used by third party tools such as type checkers, IDEs, linters, but more importantly to make your intentions clear to other humans who might be reading your code.
  2. default arguments, instead of hardcoding them inside the function. It's very important to document your functions, so that an user would not be surprised by the result (see Principle of Least Astonishment). There are of course other ways of documenting your code (see docstrings) and other ways of designing that interface (could be a class for example), but this is just to demonstrate the point.
  3. List comprehensions, which can make your code more readable by making it more declarative instead of imperative. It can be hard to determine the intention behind imperative algorithms.
  4. string interpolation, which in my experience is less error prone than concatenating.
  5. I used the pep8 style guide to name the functions, which is the most common convention in the python world.
  6. Finally, instead of printing I returned a str in the cool_para function because the code below the # DO NOT CHANGE CODE BELOW comment is printing the result of the function call.
2 of 6
1

Ideally you want to use list comprehension.

def CoolPara(letters):
  new = [i for i in text.split()]
  found = [i for i in new if letters in i]
  print(new) # Optional
  print('Word Count: ', len(new), '\nSpecial letter words: ', found, '\nOccurences: ', len(found))

CoolPara('f') # Pass your special characters through here

This gives you:

['In', 'a', 'hole', 'in', 'the', 'ground', 'there', 'lived', 'a', 'hobbit.', 'Not',
 'a', 'nasty,', 'dirty,', 'wet', 'hole,', 'filled', 'with', 'the', 'ends', 'of',
'worms', 'and', 'an', 'oozy', 'smell,', 'no', 'yet', 'a', 'dry,', 'bare,', 'sandy',
'hole', 'with', 'nothing', 'in', 'it', 'to', 'sit', 'down', 'on', 'or', 'to', 'eat;',
'it', 'was', 'a', 'hobbit-hole,', 'and', 'that', 'means', 'comfort']
Word Count:  52
Special letter words:  ['filled', 'of', 'comfort']
Occurences:  3
Find elsewhere
🌐
AskPython
askpython.com › python › list › find-string-in-list-python
Find a string in a List in Python - AskPython
February 16, 2023 - In the following example, we’ll consider a list of strings and search for the substring “Hello” in all elements of the list. ... We can use a list comprehension to find all elements in the list that contain the substring “Hello“. The syntax is as follows:
🌐
w3resource
w3resource.com › python-exercises › list › python-data-type-list-exercise-70l.php
Python: Find the items start with specific character from a given list - w3resource
Write a Python program to find items starting with a specific character from a list. ... # Define a function 'test' that takes a list 'lst' and a character 'char' as arguments def test(lst, char): # Use a list comprehension to filter elements in 'lst' that start with the specified 'char' result = [i for i in lst if i.startswith(char)] return result # Define a list 'text' containing strings text = ["abcd", "abc", "bcd", "bkie", "cder", "cdsw", "sdfsd", "dagfa", "acjd"] # Print a message indicating the purpose of the following output print("\nOriginal list:") print(text) # Define a character 'ch
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-find-string-in-list
Python Find String in List: Methods and Examples | DigitalOcean
1 month ago - For an exact match, use the in operator or index(). For a fixed substring, use in on the element or str.find(). These plain methods are faster and much easier to read than a regular expression, and they avoid hard-to-spot bugs caused by unescaped special characters.
🌐
Stack Overflow
stackoverflow.com › questions › 42614659 › how-to-find-certain-characters-in-a-list
python - How to find certain characters in a list? - Stack Overflow
You need a loop that checks if 'Ten' is in any of the list items. ... playerdeck = ['Ten of Clubs', 'Six of Diamonds', 'Five of Hearts', 'Jack of Spades', 'Five of Diamonds', 'Queen of Clubs', 'Seven of Diamonds'] s="Ten" for i in playerdeck: if s in i: print(i) print("Found") ... Thank you for this. ... Try the following. playerdeck = ['Ten of Clubs','Six of Diamonds','Five of Hearts', \ 'Jack of Spades','Five of Diamonds','Queen of Clubs','Seven of Diamonds'] for i,item in enumerate(playerdeck): if 'Ten' in item: print('Yes:',i,item)
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-find-the-string-in-a-list
Python program to find String in a List - GeeksforGeeks
July 23, 2025 - A for loop provides a manual way to iterate through the list and compare each element with the target string. ... a = ['Learn', 'Python', 'With', 'GFG'] found = False for item in a: if item == 'Python': found = True break if found: print("String ...
🌐
W3Schools
w3schools.com › python › ref_string_find.asp
Python String find() Method
Remove List Duplicates Reverse ... Python Interview Q&A Python Bootcamp Python Training ... The find() method finds the first occurrence of the specified value....