I'm writing a function that needs to go through a list of lists, collect all letters uppercase or lowercase and then return a list with 1 of each letter that it found in order. If the letter appears multiple times in the list of lists the function only has to report the first time it sees the letter.
For example, if the list of lists was [['.', 'M', 'M', 'N', 'N'],['.', '.', '.', '.', 'g'], ['B', 'B', 'B', '.','g']] then the function output should return ["M","N","g","B"].
The code I have so far seems like it could work but it doesn't seem to be working. Any help is appreciated
symbols = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
newlot = 
for i in lot:
if i == symbols:
There are a few things wrong with your code. You are using
return in the wrong place, looping only over the outer list (not over the items in the sublists) and you were appending
newlot instead of the matched item.
def get_symbols(lot): symbols = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' # You should define this OUTSIDE of the function newlot =  for i in lot: # You are iterating over the outer list only here if i == symbols: # == does not check if an item is in a list, use `in` here newlot.append(symbols) # You are appending symbols which is the alphabet return newlot # This will cause your function to exit as soon as the first iteration is over else: return None # No need for this
You can use a double
for loop and use
in to check if the character is in
symbols and isn't already in
l = [['.', 'M', 'M', 'N', 'N'],['.', '.', '.', '.', 'g'], ['B', 'B', 'B', '.','g']] symbols = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' def get_symbols(lot): newlot =  for sublist in lot: for i in sublist: if i in symbols and i not in newlot: newlot.append(i) return newlot
This is the output for your list:
>>> get_symbols(l) ['M', 'N', 'g', 'B']