Why does append print out 'NONE'? [duplicate] - python

This question already has an answer here:
Python function returns None, unclear why
(1 answer)
Closed 5 years ago.
I am trying to add items to a list with the function append. But when I print out the updated list it returns 'None'
Here's what I got so far
lists = []
count = 0
while count < 10:
count += 1
ask = input("What note do you want stored?")
lists = lists.append(ask)
print(lists)
>>> What note do you want stored? sd
None

Because you're assigning the result of lists.append back to lists. lists.append modifies its argument list and always returns None. Just do the lists.append and don't do anything with its return value.
Instead of
lists = lists.append(ask)
just do
lists.append(ask)
The append method modifies the list in place. It doesn't return the new list; in fact, it doesn't return anything at all, which is why assigning its return value to lists gets you a value of None.

Related

Is it possible to modify the list itself, not its copy? [duplicate]

This question already has answers here:
How to modify list entries during for loop?
(10 answers)
How to remove items from a list while iterating?
(25 answers)
Closed 1 year ago.
I need to write a function which removes odd numbers from a list and square remaining even numbers.
I've tried something like this:
def modify_list(l):
l1 = [i ** 2 for i in l if i % 2 == 0]
return l1
but it creates a copy from a list that I've passed to a function, whereas I need to modify the passed list itself, so the following code would result:
id(l1) == id(l) # True
I've also tried to rewrite the code using remove method, but I couldn't figure out how to square remaining elements from passed list so it would return the same list (not its copy)
def modify_list(l):
for element in l:
if element % 2 != 0:
l.remove(element)
Is it possible to change my code in order to return the same list object that I've passed, but without odd numbers and with squared even numbers?
def modify_list(l):
li = [i**2 for i in l if i % 0 == 0]
for n,i in enumerate(li):
l[n] = i
return l
lists are mutable so a direct assignment works. 'li' is a new list so will have a different id.

did I did something wrong with list and for loop in python? [duplicate]

This question already has answers here:
Why does this iterative list-growing code give IndexError: list assignment index out of range? How can I repeatedly add (append) elements to a list?
(9 answers)
Closed 1 year ago.
the thing that I am supposed to do is,
get 10 int
find the different value after doing %42 for those 10 input int
and what I thought is, like this
n = []
for i in range(10):
a = int(input())
n[i] = a%42
s = set(n)
print(len(s))
but it didn't work with a message of
----> 4 n[i] = a%42
IndexError: list assignment index out of range
and by googling I have solved this question by adding append.
n = []
for i in range(10):
a = int(input())
print("a",a)
b = a%42
print("b",b)
n.append(b)
s = set(n)
print(len(s))
** here is my question. why did my code didn't work? I thought my method's logic is solid. Is there some knowledge that I am missing about? **
thank you previously.
actually when you were trying first way you were using lists built-in dunder(magic method) which asks that there must be an element at that index before changing its value, meanwhile list is empty and hence it can't find an element whose value has to be chanced but append works something like this:
yourList += [newElement]
which is basically like list concatination.
Your code doesn't work because n is an empty list so it has no sense to assign a value to an index.
If you know the size of your list you can do:
# this works
n = [size]
n[0] = 1

evaluate a list in one line code in python [duplicate]

This question already has answers here:
How do I check if there are duplicates in a flat list?
(15 answers)
Closed 2 years ago.
I created a function to evaluate list if they have duplicates or not:
def duplica(list_to_check):
if len(set(list_to_check)) != len(list_to_check):
print('there are duplicates inside the list')
result = 0
else:
result = 1
return result
print(duplica([1, 1, 2]))
##test it:
there are duplicates inside the list
0
I want to know if there's any alternative way to evaluate the list using a code of only one line (for example lambda or map)
If you only need the value:
0 if len(set(list_to_check)) != len(list_to_check) else 1
or even better (): (provided by: Olvin Roght in the comment)
int(len(set(list_to_check)) == len(list_to_check))
With print:
(0,print('there are duplicates inside the list'))[0] if len(set(list_to_check)) != len(list_to_check) else 1

Function that takes a list and returns a list with only unique elements from the original? [duplicate]

This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(30 answers)
Closed 6 years ago.
I am working my way through the Think Python Textbook, and one of the problems it gives is to make a function that takes a list and returns a list with only unique elements from the original. What I have so far is this:
def remove_duplicates(l):
index = 0
new_l = []
dup_l = []
while index < len(l):
if l[index] in l[index+1:] or l[index] in new_l:
dup_l += [l[index]]
index = index + 1
elif l[index] in dup_l:
index = index + 1
else:
new_l += [l[index]]
return new_l
I feel like there must be a more succinct, pythonic way to write this function. Can somebody help this beginner programmer?
I assume you want to do it manually, to understand python syntax so this is a way of doing it.
Update for the comment made about only getting elements that occur only once and not removing duplicates, you can replace the if condition to if L.count(element) == 1: and it will return [1,2,4] as you were requesting.
def unique(L):
uniqueElements = []
for element in L:
if element not in uniqueElements:
uniqueElements.append(element)
return uniqueElements
print(unique([1,2,3,3,4]))
And if you want to delve further into the python syntax, you could do a list comprehension and simplify it further, while still not using the set method which loses the order of elements:
def unique(L): return [element for element in L if L.count(element) == 1]

How to compare all list itetms in Python shortest [duplicate]

This question already has answers here:
Check if all elements in a list are identical
(30 answers)
Closed 8 years ago.
I want to be able to get True if all the list's items are the same:
For example checking this list would return True:
myList = [1,1,1,1,1,1,1]
While checking this list would result to False:
myList = [2,2,2,2,2,2,1]
What would be a shortest solution without a need to declare any new variables?
Using set would drop duplicates. Then you can chcek the length, to get the number of different values.
len(set(myList)) <= 1
This works if the values are hashable.
However, if you expect to run this on long lists and expect a negative answer often, short circuiting might prove faster:
def is_unique(myList):
seen = set()
for x in myList:
seen.add(x)
if len(seen) > 1:
return False
return True

Categories