n00bprogrammer22 - 5 months ago 28

Python Question

I need to write a function that creates a tuple in the format ('letter', row, col) of all letters in a list of lists. I realized I could combine two functions I've already written to accomplish this. One function I wrote get_letters(lst) extracts all the letters from the list of lists and the other one I wrote extracts the row and column.

There must be a way to combine these two functions to accomplish this goal without creating a new one but I'm not sure how I would do it. Here's the code for these two functions:

`def get_letters(lst):`

letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

newlst = []

for sublist in lst:

for i in sublist:

if i in symbols and i not in newlst:

newlst.append(i)

#Example output: (1, 0)

return newlst

def find_letter(letter, lst):

for i in range(len(lst)):

for j in lst[i]:

if j == letter:

return i ,lot[i].index(j)

#Example output: ['A']

#New function example output: [('A', 1, 0)]

#Example input for both above functions: lst2 =

#[['.','M','M','H','H'],

#['A','.','.','.','f'],

#['B','C','D','.','f']]

Answer

```
[(letter, i, j) for i, sublist in enumerate(list_of_list) for j, letter in enumerate(sublist) if letter.isalpha()]
```

Is one way to do it with `enumerate`

. If you can't use `enumerate`

for some reason, you can just roll your own:

```
def not_enumerate_i_swear(iterable):
i = 0
for item in iterable:
yield (i, item)
i += 1
```