check if a number already exist in a list in python
You could do
if item not in mylist:
mylist.append(item)
But you should really use a set, like this :
myset = set()
myset.add(item)
EDIT: If order is important but your list is very big, you should probably use both a list and a set, like so:
mylist = []
myset = set()
for item in ...:
if item not in myset:
mylist.append(item)
myset.add(item)
This way, you get fast lookup for element existence, but you keep your ordering. If you use the naive solution, you will get O(n) performance for the lookup, and that can be bad if your list is big
Or, as @larsman pointed out, you can use OrderedDict to the same effect:
from collections import OrderedDict
mydict = OrderedDict()
for item in ...:
mydict[item] = True
Python check if list items are integers?
Try this:
mynewlist = [s for s in mylist if s.isdigit()]
From the docs:
str.isdigit()
Return true if all characters in the string are digits and there is at least one character, false otherwise.
For 8-bit strings, this method is locale-dependent.
As noted in the comments, isdigit()
returning True
does not necessarily indicate that the string can be parsed as an int via the int()
function, and it returning False
does not necessarily indicate that it cannot be. Nevertheless, the approach above should work in your case.
how to check if a list of numbers is between 2 values in python?
you can use filter to get a list but first you should change it value to float and in addition don't use list as variable name. list is a built-in operation you need and shouldn't be overridden can cause some bad side effects:
list_ = ['3,25', '3,15', '1,78', '2,10', '1,06', '1,58', '1,88', '1,19', '4,00', '2,45', '2,25', '3,00', '2,95', '2,45', '2,30', '1,52', '1,96', '6,50', '4,20', '1,27']
list_number = [float(number.replace(",",".")) for number in list_]
found = filter(lambda x: 1.50 <= x <= 2.50, list_number)
for value in found:
print(value)
But of course if you don't want to cast it to float you can do the following as well without a problem:
found = filter(lambda x: "1,50" <= x <= "2,50", list_)
The output will be the same. (except in one it has "," and ending zero and the other result use "." without ending zeros)
Output:
1.78
2.1
1.58
1.88
2.45
2.25
2.45
2.3
1.52
1.96
How to check if list is a list of integers
Try:
all(isinstance(n, int) for n in a) # False for your example
python: Check if all the elements in the list are only numbers [closed]
You can use the type
function in conjunction with an all
to gather the results.
l1 = [1, 2, 3, 4, 5, 6]
l2 = ["abc", "xyz", "pqr"]
print(all(type(e) in (int, float) for e in l1))
print(all(type(e) in (int, float) for e in l2))
Results in:
True
False
Python - Check if all n numbers are present in a list
There is no need to use zip
with multiple zip
function.You can use sorted
:
if sorted(l)==list(range(max(l)+1))
example :
>>> sorted(l)==list(range(max(l)+1))
False
>>> l= [0,2,1,7,6,5,4,3]
>>> sorted(l)==list(range(max(l)+1))
True
How to check if a number is in a list of lists?
You'll have to loop through each list in maze
and check whether there is a 0 or not:
if not any(0 in i for i in maze):
...
The great thing about the any()
function is that it stops looping through maze
once it finds a 0.
Python: check if a number is already in a grid of list of lists
If the grid is a list of lists you can use a for/else block:
def possible(n, grid):
for x in grid:
if n in x:
return False
else:
return True
grid = [[0, 2, 3],
[4, 0, 5],
[6, 7, 0]]
print(possible(1, grid)) # ---> True
print(possible(2, grid)) # ---> False
Related Topics
How to Run Python Script from Another Machine Without Installing Imported Modules
Remove Partial String from Dataframe With Pandas
How to Install Pip for a Specific Python Version
Finding the Most Frequent Character in a String
Calculating the Area Under a Curve Given a Set of Coordinates, Without Knowing the Function
Get the First Item from an Iterable That Matches a Condition
Adding a Data File in Pyinstaller Using the Onefile Option
Keep Other Columns When Doing Groupby
Broadcast One Channel in Numpy Array into Three Channels
No Output Displays When Execute Python File
How to Restart a Program Based on User Input
How to Extract a Value (I Want an Int Not Row) from a Dataframe and Do Simple Calculations on It
How to Name a File by a Variable Name in Python
Check If a Key Exists in a Bucket in S3 Using Boto3