Python Forum
Why is my string empty? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why is my string empty? (/thread-32243.html)



Why is my string empty? - karabakh - Jan-30-2021

Hi,

I want to create a loop that would attach the ids from a certain dictionary to a string, space between ids.

import string

letters = list(string.ascii_lowercase)

dic = {}
lista = []
strin=()
final=[]
for id, i in enumerate(letters):
    dic[id]=i

def alphabet_position(text):
    for i in text:
           strin=' '.join([str(k+1)+v for k,v in dic.items() if v == i.lower()])
But the 'strin' returned is empty. It works fine when I remove the condition in the list comprehension (if v==i.lower())).

Why? Thank you


RE: Why is my string empty? - bowlofred - Jan-30-2021

In the first place, why are you building a dictionary that maps from position to letter, but then using it to look from letter to position? My first thought is that you should build the dictionary in that direction and use it naturally. What this program does is use the dictionary like a list. It would be simpler to just loop through the original string.ascii_lowercase than loop through the dictionary.

Next, while alphabet_position loops over the characters in the input, it overwrites strin each time through the loop. So only the final character is kept in that variable.

Finally, the function has no return call. So if you are calling it, you will always get back None


RE: Why is my string empty? - karabakh - Jan-30-2021

(Jan-30-2021, 02:24 AM)bowlofred Wrote: In the first place, why are you building a dictionary that maps from position to letter, but then using it to look from letter to position? My first thought is that you should build the dictionary in that direction and use it naturally. What this program does is use the dictionary like a list. It would be simpler to just loop through the original string.ascii_lowercase than loop through the dictionary.

Next, while alphabet_position loops over the characters in the input, it overwrites strin each time through the loop. So only the final character is kept in that variable.

Finally, the function has no return call. So if you are calling it, you will always get back None

Thanks for the reply. Your first point is great and makes sense.

For your latter part of the comment, why does it overwrite? When I add the ids to some list, the list is not being overwritten.
Here's the code with adding to the list. I also added returns.

def alphabet_position(text):
    for i in text:
        lista.append([k+1 for k,v in dic.items() if v == i.lower()])
        strin=' '.join([str(k+1)+v for k,v in dic.items() if v == i.lower()])



RE: Why is my string empty? - deanhystad - Jan-30-2021

Strings are immutable. They cannot be modified.


RE: Why is my string empty? - bowlofred - Jan-30-2021

(Jan-30-2021, 02:29 AM)karabakh Wrote: When I add the ids to some list, the list is not being overwritten.

That's because you're using a method of the list that modifies it, like list.append(). What you're not doing is reassigning the list each time through like list = [new_item].

With strings, you'd instead assign the existing part and the new part to a variable. Something like:

full_string = ""
for s in list:
    full_string += s  # Keeps the existing portion each time through the loop.



RE: Why is my string empty? - karabakh - Jan-30-2021

(Jan-30-2021, 03:52 AM)bowlofred Wrote:
(Jan-30-2021, 02:29 AM)karabakh Wrote: When I add the ids to some list, the list is not being overwritten.

That's because you're using a method of the list that modifies it, like list.append(). What you're not doing is reassigning the list each time through like list = [new_item].

With strings, you'd instead assign the existing part and the new part to a variable. Something like:

full_string = ""
for s in list:
    full_string += s  # Keeps the existing portion each time through the loop.

Thank you. I also discovered extend which seems to be what you are mentioning here.