0
def send_messages(message):
    while message:
        print(message)
        message.pop()
        sent_messages.append(message)

message = ['hello', 'i am on my way', 'cant talk right now']
sent_messages = []

send_messages(message)

print(message)
print(sent_messages)

I am learning python and having trouble trying to print 2 lists. I am trying to move values from one list to another using pop.() and append.() method However, when I run the program, it only shows me empty square brackets of the sent_messages list with no values in them.

Hussain
  • 9
  • 2
  • Fast fix: `sent_messages.append(list(message))`. This happens because not copy of `message` is appended to `send_messages` but kinda pointer to it, so that's why it happens. – fas Apr 13 '20 at 16:50
  • Hey! you can just set the `messaged.pop()` as a variable and then append that to the sent_messages list. – Rohan Nagavardhan Apr 13 '20 at 16:51
  • @fas No this isn't a fix at all, here message is his list, you will be appending the whole list instead of the poped msg. – Tic Apr 13 '20 at 16:57
  • @Hussain As you are learning python, notice that using the pop() function, you are reversing the list and destructing the message list, depending on what you want to do, this might be fine. Also, to make your code easier to follow, you could define the sent_messages in the send_messages and return it from the function. – Tic Apr 13 '20 at 17:04
  • what do you think this does: `sent_messages.append(message)`? – njzk2 Apr 13 '20 at 17:31
  • @njzk2 It appends the value in message to the end of sent_messages – Hussain Apr 13 '20 at 17:49
  • @Hussain right. The issue being that `message` is a list of strings, and that it becomes empty with time. I assume that you wanted to append the value popped from the list? – njzk2 Apr 13 '20 at 20:41

2 Answers2

-1

Try this

send_messages=[]
def send_messages(message):
    while message:
        sent_messages.append(message.pop(0))

message = ['hello', 'i am on my way', 'cant talk right now']
sent_messages = []

send_messages(message)

print("message list" ,message)
print("sent_messages list", sent_messages)

Output

message list []
sent_messages list ['hello', 'i am on my way', 'cant talk right now']
FAHAD SIDDIQUI
  • 565
  • 2
  • 20
-1

you are appending to the list sent_messages only references of your list message while you are removing all the elements from your list message, in the end, you will have references to an empty list (since message list will be empty)

you could use:

message = ['hello', 'i am on my way', 'cant talk right now']

sent_messages = message.copy()
message = []


print(message)
print(sent_messages)

output:

[]
['hello', 'i am on my way', 'cant talk right now']

if you want to learn Python and use list.pop and list.append you could use:

message = ['hello', 'i am on my way', 'cant talk right now']

def send_messages(message):
    new_list = []
    while message:
        new_list.append(message.pop())  # keep in mind that .pop will pop out the last element

    return new_list[::-1] # reverse to keep message elements order

sent_messages = send_messages(message)
print(message)
print(sent_messages)

output:

[]
['hello', 'i am on my way', 'cant talk right now']
kederrac
  • 15,339
  • 4
  • 25
  • 45