This question already has answers here:
Finding if any element in a list is in another list and return the first element found
(6 answers)
Closed 4 years ago.
I want to check if all the numbers inside "check" is inside a given array "lists", something like this:
import sys
lists = [1,2,8,8,8,8,8,8,8,8,8,8]
check = [1,2,3]
for check in lists:
print True
sys.exit()
print False
However, this prints True all the time except if I make lists = []. Does any one know how this will work? Thanks in advance!
Updated working
s = [True if lists[idx: idx+3] == check else False for idx, item in enumerate(lists[:-2])]
if any(s):
print(True)
else:
print(False)
it's keep printing True because you're printing True inside the loop, and if the list is [], it can't iterate because it's empty, so try just:
print(all(i in lists for i in check))
So whole code is:
lists = [1,2,8,8,8,8,8,8,8,8,8,8]
check=[1,2,3]
print(all(i in lists for i in check))
Update:
print(all(i in lists for i in check) and [lists.index(i) for i in check]==range([lists.index(i) for i in check][0],[lists.index(i) for i in check][-1]+1))
Related
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.
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
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 4 years ago.
I'm trying to create a function with two inputs that can append a value into a list of choice. I'm starting of with two lists to get the code working before I build up.
a = []
b = []
def func(z,x):
if z == a:
a.append(x)
elif z == b:
b.append(x)
print(a,b)
For some reason it appends to the first list, then the second, no matter which I select. I've only just started to learn python, so I may have missed something basic.
== when comparing two lists sees if they contain the same items. An empty list is equivalent to another empty list, so the first time that function is called, it will always append to a. What you could use instead is is (if z is a:), but a much better way is to ignore a and b, and just use z directly:
def func(z, x):
z.append(x)
print(a, b)
which brings up doubts as to why this function is needed...
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.
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