This question already has answers here:
Apply function to each element of a list
(4 answers)
Appending to a list comprehension in Python returns None
(3 answers)
Closed 1 year ago.
I am trying to minimize as much as possible my coding and trying to solve problems in different ways and i wrote this code which gets a list of numbers and it returns the square root of each one number in list
def square_roots(l):
squareRoots = []
result = [squareRoots.append(math.sqrt(i)) for i in l]
return result
l=[1,2,3]
print(square_roots(l))
The only problem is that it returns [None, None, None] instead of the square root of each number in the array.
What's wrong with it?
list.append() (i.e. squareRoots.append()) returns None. Remove that part.
def square_roots(l):
result = [math.sqrt(i) for i in l]
return result
You might want to read Why does append() always return None in Python?
Related
This question already has answers here:
How can I use `return` to get back multiple values from a loop? Can I put them in a list?
(2 answers)
Closed 7 months ago.
this function is supposed to return the list in reverse but instead, it is only returning the last element of the list. I would like to know what is the issue.
def reverse_list(lt):
for f in lt[::-1]:
return f
lt = [1,2,3,4]
print(reverse_list(lt))
def reverse_list(lt):
return lt[::-1]
lt = [1,2,3,4]
print(reverse_list(lt))
This question already has answers here:
Finding median of list in Python
(28 answers)
Closed 3 years ago.
I have a problem that one of the things i need to include is a subscript of median, but i have no clue what that means
I have tried most things but again i have no clue what subscript of median means.
def median(a):
a=a.sort()
a=len(a)/2
return a
def main():
print(median([3,1,2]))
print(median([4,3,2,1]))
print(median([1,5,3,2,4]))
print(median([6,5,1,2,3,4]))
main()
I expect it to print out the median of the numbers if it gets two i need the lesser... We cant use average.
You're returning the middle index, not the value of the element at that index.
Also, a.sort() modifies the list in place, it doesn't return the sorted list; a = a.sort() sorts the list and then sets a to None.
def median(a):
s = sorted(a)
middle = int(len(s)/2)
return s[middle]
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 4 years ago.
I'm using the code below:
a = ('Monty Python', 'British', 1969) #a is a tuple
b=list(a) #this should convert it to a list if I'm not wrong
print(b) #the output till here is okay
c=b.append("abcd")
print(c) # the output for this is None
Can anyone explain why am I unable to edit after converting the tuple to a list??
.append() does not return a list.
You are doing c = b.append("abcd"), this makes no sense because b.append() does not return a list, it returns none.
Try print(type(b.append("abcd"))) and see what it prints. So as you can see python is working correctly.
Things like .append() .pop() do not return a new list, they change the list in memory.
This is called an inplace operation I believe
You're printing c whose job is to append. Print b instead, that's your list.
This question already has answers here:
calculate length of list recursively [duplicate]
(2 answers)
Closed 7 years ago.
So, my goal is to count the number of arguments in a list using recursion. However, as I'm only to use one argument in the function call, I don't know how to solve it without a second "count" argument.
So far I have this, where I accumulate the 1's together.
def countElements(a):
if a==[]:
return []
else:
return [1] + countElements(a[1:])
def main():
a=[3,2,5,3]
print(countElements(a))
main()
Instead of returning an empty list in the base case, return 0, and instead of [1] + countElements(a[1:]), return 1 + countElements(a[1:]). That way, you're just keeping the running count instead of getting a list back.
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 7 years ago.
Is there any difference between these two ways of returning lists?
Initially the list is empty.
my_list = []
method 1:
my_list.append(1)
return my_list
method 2
return my_list.append(1)
Actually, the second method is returning an empty list for me. Please clarify why it is happening like this
When you type the following:
return my_list
You are returning a list object. When you type the following:
return my_list.append(something)
You are returning the result of that method call. In the case of .append() that method is void, so you are effectively returning nothing. If the method .append() appended the argument you pass to it and then returned the modified list itself then you could do it, but that isn't the case.