Python - Need to extract the first and last letter from a word string
<p>I've been stuck with this for a while. basically the <code>get_text_value(text)</code> code below is passed a string of words and it extracts the first and last letter of each word, matches it up with <code>letter_values</code> and gives the final value based on the sum of the numbers the first and last words matched up with. So far I've only been able to "extract" the first and last letter of the whole string and not each individual word. 
I know I'm meant to convert the string into a list, but I'm not entirely sure how to go about it. Any help would be greatly appreciated. Thanks!</p>

<pre><code>def get_text_value(text):
     letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
     letter_values = [1, 4, 2, 3, 1, 4, 3, 4, 1, 7, 7, 4, 6, 6, 1, 3, 9, 2, 2, 2, 1, 8, 5, 9, 9, 9]
     text_value = 0
     text = text.lower()
     for i in text:
          if text[0] and text[-1] in letters:
               letters_index_1 = letters.index(text[0])
               letters_index_2 = letters.index(text[-1])
               text_value = letter_values[letters_index_1] + letter_values[letters_index_2]

     return text_value

def test_get_text_value():
     print("1. Text value:", get_text_value("abracadabra")) 
     print("2. Text value:", get_text_value("a b"))
     print("3. Text value:", get_text_value("enjoy Today"))
     print("4. Text value:", get_text_value(""))

     text = "Be yourself everyone else is already taken"
     text_value = get_text_value(text)
     print("6. Text:", text)
     print("6. Text value:", text_value)
</code></pre>

<p>do like this:</p>

<pre><code>def get_text_value2(text):
    letters = [chr(i) for i in range(ord('a'), ord('z')+1)]
    letter_values = [1, 4, 2, 3, 1, 4, 3, 4, 1, 7, 7, 4, 6, 6, 1, 3, 9, 2, 2, 2, 1, 8, 5, 9, 9, 9]
    alpha_value_dict = dict(zip(letters, letter_values))
    formatted_text = text.lower()
    return alpha_value_dict.get(text[0], 0) + alpha_value_dict.get(text[-1], 0)
</code></pre>

<p>or if you want keep the same logic of return value with your origional function, you can do:</p>

<pre><code>def get_text_value2(text):
    letters = [chr(i) for i in range(ord('a'), ord('z')+1)]
    letter_values = [1, 4, 2, 3, 1, 4, 3, 4, 1, 7, 7, 4, 6, 6, 1, 3, 9, 2, 2, 2, 1, 8, 5, 9, 9, 9]
    alpha_value_dict = dict(zip(letters, letter_values))
    formatted_text = text.lower()
    try:
        return alpha_value_dict[text[0]] + alpha_value_dict[text[-1]]
    except:
        return 0
</code></pre>

python string list python-3.x