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

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

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.

List comprehension to evaluate to true if list contains a value [duplicate]

This question already has answers here:
Fastest way to check if a value exists in a list
(11 answers)
Closed 1 year ago.
I have a long list of values and want a list comprehension to evaluate to True (and print "True" only once if any value in the list is the integer 1).
I can print "True" for each instance a 1 is found but cannot see how to just have it return a single True.
Code
a = [0,0,1,1,0,1]
b = [print("True") for i in a if i == 1]
print('\n')
#c = [print("True") if any i in a is True] # doesn't work, syntax error
d = [print("TRUE") if any(i == 1)]
Did you mean to convert the resulting list to bool()?
a = [0,0,1,1,0,1]
b = bool([i for i in a if i == 1])
print(b)
if your list only contains zeros and ones you could just print(any(a))
otherwise you could do this
a = [0,0,1,0,2,0]
b =[x==1 for x in a]
print(any(b))
returns True

My .remove() isn't removing all the numbers from the list even though I'm passing it through an iterator. I can't seem to find the issue [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
How to remove list elements in a for loop in Python? [duplicate]
(6 answers)
Closed 3 years ago.
def thirdMax(nums):
highest = max(nums)
for i in nums:
if i == highest:
nums.remove(i)
print(nums)
secondHighest = max(nums)
for i in nums:
if i == secondHighest:
nums.remove(i)
thirdHighest = max(nums)
return thirdHighest
thirdMax([1,2,3,3,3,3,3,3,4,4,4,4,4,4,4])
My code is supposed to return the third highest distinct number. My code isn't seeming to work accordingly.
Sort the list (to make sure). Convert to a set. Convert back to a list and take the 3rd from the right.
l = [1,2,3,3,3,3,3,3,4,4,4,4,4,4,4]
l.sort()
s = set(l)
l = list(s)
third = l[-3]
You can combine some of these steps, but I wrote them all out for clarity :)

Get the number of times the number "1" appears in my list, without using if statements or empty lists in for loop? [duplicate]

This question already has answers here:
How do I count the occurrences of a list item?
(29 answers)
Closed 3 years ago.
How do you get the number of times the number 1 appears in the following list:
list = [1,2,3,4,5,1,1]
I have tried the following and it works, but I want to do it without creating an empty list and the if statement, but what I really want is a count of the number of times 1 appears instead of a list with only 1s and then count them.
list = [1,2,3,4,5,1,1]
list_1s = []
for i in list:
if i == 1:
list_1s.append(i)
else:
pass
n_1s = len(list_1s)
print(n_1s)
lst = [1,2,3,4,5,1,1]
Using list comprehensions to solve it -
count_1s = len([i for i in lst if i==1])
print(count_1s)
3

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