Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie lists problem
#1
Hi,

I'm trying to learn Python by myself, and while trying some code to learn more about lists I tried something that didn't output what I did except to... Can you help me and furthermore explain me the reason why it didn't work.


empty_list = []
first_var = 1

filled_list = empty_list.append(first_var)
print(filled_list)
It prints me out "None" instead of [1]…

Thanks a lot
Reply
#2
The append method doesn't return anything (or rather it returns None), it modifies the list you appended to. You want to print empty_list.

This is an important thing about lists: they can be changed in place. You'll note that string can't, which is why most of the string methods return a new string.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Okay! If I've understood It's like if I wrote:

filled_list = None
Because it doesn't take in count the append() method to print it out ?

Thank you very much :)
Reply
#4
Well, it does take into account the append method. That's where the None came from. It's just that the actual appending is done to empty_list. That is, after line 4, empty_list is [1] and filled_list is None.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
empty_list = []
first_var = 1

filled_list = empty_list.append(first_var) # here it will append to empty_list and append returns None which is assigned to filled_list
print(filled_list)
print(empty_list)
Output:
None [1] >>>
So your code is equivalent to
empty_list = []
first_var = 1
empty_list.append(first_var)
filled_list = None
print(filled_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
#6
Okay, it's clearer to me now ! Thanks you both for helping newbies like me we appreciate that :D see you soon ^^'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  very newbie problem on text file zapad 2 232 Apr-12-2024, 06:50 PM
Last Post: zapad
  problem with print lists MarekGwozdz 4 699 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  Newbie first problem cima_hurdle 8 3,469 Feb-09-2022, 10:02 PM
Last Post: cima_hurdle
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,387 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,294 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Newbie query regarding lists jazmad 5 3,526 Oct-13-2018, 07:58 PM
Last Post: jazmad
  Newbie Ubuntu script problem Kloontor 6 3,550 Sep-24-2018, 03:51 PM
Last Post: Kloontor
  Problem with Lists in Python DEAD_BOT 2 3,077 Aug-26-2018, 06:09 AM
Last Post: DEAD_BOT
  Hey guys, help a newbie out with a list problem. sdezigner 2 2,684 Feb-15-2018, 06:06 PM
Last Post: sdezigner
  newbie problem Pedroski55 3 4,503 Jul-04-2017, 07:31 AM
Last Post: buran

Forum Jump:

User Panel Messages

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