Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List operation error
#1
Hello everyone,
I recently registered in order to ask for help. Being a total newbie, I decided to practice with list operations, and I wanted to create a very simple program, simply using "append" and "pop".
random_list=[1, 2, 4, 8]
print(random_list.append("num"))
print(random_list.pop(1))
The bad news is, though, I'm not getting the output i wanted: while it should be [1, 2, 4, 8, "num"] and [1, 4, 8], what I get is:
None ; 2

What am I doing wrong?
Thanks in advance for your generous help, dear coders!
Reply
#2
Hello and welcome to Python and the forums!
I wouldn't say you are doing anything wrong :)
You are not printing lists, but results (or rather return values) of the methods (functions belonging to list objects).
append method returns None, and pop method returns the popped item from the list (usually main purpose of using pop in the first place).
Reply
#3
random_list=[1, 2, 4, 8]
random_list.append("num")
print(random_list)
random_list.pop(1)
print(random_list)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thanks to you two for the quick answers (:
Buran's code is indeed working, so I think that's it!
Have a good day (:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error in Matrix operation poorikanna 0 2,749 Oct-23-2017, 12:14 PM
Last Post: poorikanna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020