This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 8 years ago.
Take these two sample code statments:
result = []
return result.append(feed.entries[0])
&
result = []
result.append(feed.entries[0])
return result
The first gives me an error because the method that result is passed to complains of a NonType not being iterable. Why is this? To me both statements are equivalent
The append method of a list does not return anything
>>> a = []
>>> type(a.append(12))
<type 'NoneType'>
So when you're doing:
return result.append(feed.entries[0])
You're actually returning None in all cases, whereas when you do:
result.append(....)
return result
you're returning the list after it has been mutated (modified) hence giving a the expected result.
Related
This question already has answers here:
Why do these list methods (append, sort, extend, remove, clear, reverse) return None rather than the resulting list?
(6 answers)
Closed 1 year ago.
Help me understand why this decorator does not add a value to the end of the list that is formed in the do_list () function.
I get the result of the function working with the decorator None
def dec_do_list(func):
def wrapper(arg: int):
result = func(arg).append(4)
return result
return wrapper
#dec_do_list
def do_list(arg: int):
import random
result = []
for i in range(arg):
result.append(random.random())
return result
print(do_list(4))
Thank you in advance!
P.S.
I do this for educational purposes, in order to better understand the Decorator pattern
result = func(arg).append(4)
append doesn't return a new list. It modifies the list in-place. You are getting the result as the return value of append and then returning it so at the end the returned value is None.
You can change it to:
result = func(arg)
result.append(4)
return result
or, in one line:
return func(arg) + [4]
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 1 year ago.
I have been studying coding on my own and some how I have been stuck, not being able to compile the same solution:
my code:
def append_size(lst):
num = len(lst)
lst = lst.append(num)
return lst
print(append_size([23, 42, 108]))
solution:
def append_size(lst):
lst.append(len(lst))
return lst
print(append_size([23, 42, 108]))
first one gives out "None" and second one gives out the list.
tell me why.
Thank you🤘
Its because in the first one, you assign lst the result of the call lst.append()
The .append() method returns None so assigning it to a variable and returning it... Would return None
While in the second case. No such assignment takes place
# first code
lst = lst.append(x) # end up assigning None to lst
#second code
lst.append(x) # no unwanted assignments
.append() wont return anything (just None), it just modifies a list. So when you do lst = lst.append(list) you are basically assigning None to lst.
Try this instead
def append_size(lst):
num = len(lst)
lst.append(num)
return lst
print(append_size([23, 42, 108]))
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 2 years ago.
How can I create a Python custom function to get list of strings having length more than a number n when we pass number n and list of strings?
I tried using this function but it returns None:
lst=['shiva', 'patel', 'ram','krishna', 'bran']
filtered_list=[]
def word_remove(n, lst):
for i in lst:
if len(i)>n:
return filtered_list.append(i)
print(word_remove(4,lst))
The output is :
None
append method on a list does not return any value. Hence the None type.
Append function has no return type. Try instead this.
def word_remove(n, lst):
for i in lst:
if len(i) > n:
filtered_list.append(i)
return filtered_list
lst = ['shiva', 'patel', 'ram','krishna', 'bran']
filtered_list=[]
def word_remove(n, lst):
for i in lst:
if len(i)>n:
filtered_list.append(i)
return filtered_list
print (word_remove(4,lst))
Output:
['shiva', 'patel', 'krishna']
The method append does not return anything. So you need to return the whole filtered_list.
This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
How can I iterate over overlapping (current, next) pairs of values from a list?
(12 answers)
Closed 4 years ago.
I'm trying to make a function that takes an input list parameter and do some math inside, returning a new list with the data collected from the math, but I'm always getting an empty list as a result. How do I need to pass the list to the function in order to get this to work?
inputArray = [12,6,7,3,8]
def derive (self, *inputArray):
outputarray = []
for i in inputArray:
operatorA = inputArray[i]
operatorB = inputArray[i+1]
if operatorA > operatorB:
operation = operatorA - operatorB
outputarray.append(operation)
else:
operation = operatorB - operatorA
outputarray.append(operation)
print(outputarray)
derive(inputArray)
You were misusing for. To iterate through index, you should use for i in range(len(inputArray) - 1). This will iterate through the list of indexes.
Also, you created a function that requires 2 arguments, but call it with just one, so I removed self.
And to finish, I believe you used the * in an attempt to refer to the string address. That's C/C++ syntax, but won't work on python. Instead, you can return the value to the original string:
inputArray = [12,6,7,3,8]
def derive (inputArray):
outputarray = []
for i in range(len(inputArray)-1):
operatorA = inputArray[i]
operatorB = inputArray[i+1]
if operatorA > operatorB:
operation = operatorA - operatorB
else:
operation = operatorB - operatorA
outputarray.append(operation)
print(outputarray)
return(outputarray)
inputArray = derive(inputArray)
This question already has answers here:
How can I get a reversed copy of a list (avoid a separate statement when chaining a method after .reverse)?
(11 answers)
Closed 6 years ago.
I can't figure out why my if-else statement doesn't run as expected. I've tried to create code to test for a palindromic string. My print functions show me that the reverse method works, but when I get to the comparison stage I can't get it to return True.
Here is the code:
def is_palindrome(a):
myList = []
for i in a:
myList.append(i)
print myList
new = myList.reverse()
print myList
print new
if myList == new:
return True
else:
return False
print is_palindrome("radar")
This returns False. I have also tried changing the if statement to if myList is new: but unfortunately it still returns False.
Any insight appreciated!
list.reverse() is in-place, meaning it reverses the list it is called upon but doesn't return anything.
The line print new should print None, and therefore myList == new will be False.
Instead, use [::-1] which is not in-place and returns a new, reversed list, or use an easier way to detect a palindrome, for example:
def is_palindrome(iterable):
return iterable == iterable[::-1]