Python's insert returning None?
Mutating-methods on lists tend to return None
, not the modified list as you expect -- such metods perform their effect by altering the list in-place, not by building and returning a new one. So, print numbers
instead of print clean
will show you the altered list.
If you need to keep numbers
intact, first you make a copy, then you alter the copy:
clean = list(numbers)
clean.insert(3, 'four')
this has the overall effect you appear to desire: numbers
is unchanged, clean
is the changed list.
Why does insert change my list to None type?
It's an in-place insertion with no return value. Use this, instead.
animals.insert(2, "cobra")
print(animals)
The insert function is returning None
insert()
modifies the list in place but does not return a value. Typically with functional programming you avoid side effects and treat most data structures as immutable, so insert()
is probably the wrong choice here. Instead of mututaing the returned list, you can simply return the result of recursion along with the new value:
def dectob(num):
if num==0:
return []
hnum = int(num/2)
if num - hnum == hnum:
asn="0"
else:
asn="1"
return dectob(hnum) + [asn]
int(''.join(dectob(6666))) == 1101000001010
# True
Why does append() always return None in Python?
append
is a mutating (destructive) operation (it modifies the list in place instead of of returning a new list). The idiomatic way to do the non-destructive equivalent of append
would be
>>> l = [1,2,3]
>>> l + [4]
[1,2,3,4]
>>> l
[1,2,3]
to answer your question, my guess is that if append
returned the newly modified list, users might think that it was non-destructive, ie they might write code like
m = l.append("a")
n = l.append("b")
and expect n
to be [1,2,3,"b"]
Add an element to a list, the output is none
Adding to the replies that you got, if it is the list b that needs to be updated but not the list a, you should proceed as such:
a =[1,2,3]
b = list(a)
b.insert(1,20)
print(a, b)
Trouble inserting in Python (None vs. empty list)
list.insert
returns None, not the modified list.
This requires helper
to be changed to read
def helper(list1, list2, result):
if not list1:
return result + list2
elif not list2:
return result + list1
elif list1[0] < list2[0]:
return helper(list1[1:], list2, result + [list1[0]])
else:
return helper(list1, list2[1:], result + [list2[0]])
Note the changes to the two base cases. None
and the empty list []
are not the same thing. The pythonic way of testing if a list is empty is to treat the list as a Boolean value: empty lists are False
, all others are True
.
And as the others have noticed before me, you need to explicitly return the return value of helper
in linear_merge
.
Related Topics
Sorting a List of Dot-Separated Numbers, Like Software Versions
How to Unzip a List of Tuples into Individual Lists
How to Apply Piecewise Linear Fit in Python
Coalesce Values from 2 Columns into a Single Column in a Pandas Dataframe
Why Does Str(Float) Return More Digits in Python 3 Than Python 2
How to Use 'Else' in a List Comprehension
Decorating Class Methods - How to Pass the Instance to the Decorator
Pandas: Conditional Rolling Count
How to Print One Character at a Time on One Line
Defining a Discrete Colormap for Imshow in Matplotlib
Python's Insert Returning None
Change User-Agent for Selenium Web-Driver
Could Not Find a Version That Satisfies the Requirement Tensorflow
How to Determine Whether a Year Is a Leap Year
Python Regex Match or Operator