Python Forum
I don't understand this result
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't understand this result
#1
>>> f = [].extend(['spam', 32, 44])
>>> type(f)
         <class 'NoneType'>
Reply
#2
>>> f = [].extend(['spam', 32, 44])
>>> f
>>> repr(f)
'None'
You need to define a list fist,then extend it.
>>> lst = []
>>> lst.extend(['spam', 32, 44])
>>> lst
['spam', 32, 44]
Reply
#3
extend changes the list in place (and thus return None)

>>> f = [].extend(['spam', 32, 44])
>>> type(f)
<type 'NoneType'>
>>> f=[]
>>> f.extend(['spam', 32, 4])
>>> type(f)
<type 'list'>
>>> print f
['spam', 32, 4]
Reply
#4
>>> f = [] + ['spam', 32, 44]
>>> type(f)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Two same codes, different result. Please help to understand. vickyprabhat 5 3,590 Mar-24-2018, 11:33 PM
Last Post: vickyprabhat
  I don't understand this result Ponomarenko Pavlo 5 3,822 Mar-27-2017, 02:04 PM
Last Post: buran

Forum Jump:

User Panel Messages

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