1

I have code that looks like this:

for item in items:

    if item.data["position"] == '5':
        item.delete() 
        continue
    elif item.data["lang"] == 'SPA':
        message_body = my_message_spa[item.data["position"]]
    else:
        message_body = my_message_eng[item.data["position"]] # English (default)

    message = client.messages.create(
        item.data["phone_number"],                    
        body=message_body,
        from_="xxxx"                           
    )
    # increment the position for this user
    item.data["position"]+=1

This code is meant to send messages to an user. Items is a list of dictionaries that has an user's phone number, date, language, and position. Position refers to their position in the sequence of messages. I would like to check the position of a user and if they have already received 5 messages, then it should be removed from the list and continue on to the next user. If the position of the user is <5 then it should go into the else if statements and send messages according to the conditions met.

The list of dictionaries has the following structure:

{'phone_number': '+1234567890', 'position': 5, 'lang': 'ENG', 'Date': '2018-08-17 00:03:46'}
{'phone_number': '+0987654321', 'position': 2, 'lang': 'ENG', 'Date': '2018-12-18 07:10:47'}

The code worked fine previously and the new part I am adding/testing is the if statement checking for position but it seems like that if statement does not get invoked at all and goes straight to the below elif checking for 'lang' and sends the message and increments the position for the user. In my case, the user's position gets incremented to 6.

Elar
  • 880
  • 8
  • 18
kevingnauh
  • 35
  • 3

1 Answers1

3

You seem to be comparing the value incorrectly. You are trying to compare a string "5" to an integer 5.

if item.data["position"] == '5':

note the quotes around 5.

For example:

print(1 == "1")
print(1 == 1)

returns False for the first one, and True for the second one.

You also should not delete items from something like a list when iterating over it. Instead try creating a copy or using list comprehension.

Turtvaiz
  • 66
  • 2
  • 5
  • Sorry, I am fairly new to python and got handed this project. Thank you for the help. Could you expand on your last sentence on why I should not delete items from a list? Or what did you mean by making a copy or using list comprehension? The examples I found online shows list comprehension being used to iterate over a string. – kevingnauh Dec 18 '18 at 22:18
  • I mean that you cannot modify the thing's index you are iterating over. The for loop is going to think there are x items in the list, and if you remove one, it's still going to try to access that many items. You can [make a copy of the list](https://stackoverflow.com/questions/6022764/python-removing-list-element-while-iterating-over-list) and only edit the original list, which does not mess up the for loop. Or [list comprehension](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating), e.g. somelist = [x for x in somelist if not x["position"] == 5] – Turtvaiz Dec 19 '18 at 09:58