The Python Oracle

Python - check if a letter is in a list

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Quirky Dreamscape Looping

--

Chapters
00:00 Python - Check If A Letter Is In A List
00:38 Answer 1 Score 20
01:07 Answer 2 Score 0
01:25 Answer 3 Score 0
01:57 Accepted Answer Score 0
02:32 Thank you

--

Full question
https://stackoverflow.com/questions/2635...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #string #list #recursion

#avk47



ANSWER 1

Score 20


I think the most pythonic approach would be to use

def find_letter(letter, lst):
    return any(letter in word for word in lst)

The beauty of this is that it iterates over lst and returns as soon as one of the words in that list contains letter. Also, it doesn't need to recurse.

This returns False instead of 0 if lst is empty, though (unlike your program) but since False evaluates to 0 anyway (and vice versa), that's not really an issue.




ANSWER 2

Score 0


You want to find the Membership

Please refer this code to resolve your problem. Suppose

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];

print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
print 3 in list2    

Output:

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]
True



ANSWER 3

Score 0


Just realized OP wants to check A string only You can define a function and match the string in a list recursively like this:

def plist(lst, n):
    # loop inside the list
    for each in lst:
        # if "each" is also a list (nested), check inside that list, too
        if isinstance(each, list):
            # this will reuse your "plist" and loop over any nested list again
            plist(each, n)
        # if n matches any element in the list, return True
        if any(n in each_letter for each_letter in each):
            return True
    # if after looping over your list + nested list, return False if no match is find
    else:
        return False

>> lst = ['o', ['hello', 'c', 'bye']]
>> plist(lst, 'o')
>> True
>> plist(lst, 'h')
>> True
>> plist(lst, 'z')
>> False

Hope this solves your problem.




ACCEPTED ANSWER

Score 0


Here is your error

def find_letter(lst):  # You receive your list as lst
    lst=['o','hello', 1]  # Opppsss! You override it. It does not matter what you receive with lst above, now its value is ['o','hello', 1]
    n='o'

So in find_letter(lst[0:]), you use list slicing, but on lst=['o','hello', 1] line, you override it again and you always execute your search on the first element of the list.

n = "o"  # you can set this too, but this is optional
def find_letter(lst):
    # do not set a value to lst in here

    if not lst:          
        return 0

    elif lst[0] == n:  # You checked first element in here
        return True

    elif find_letter(lst[1:]):  # since you checked the first element, skip it and return the orher elements of the list
        return True

    else: 
        return False

lst = ['o','hello', 1]
print find_letter(lst)