DEV Community

Discussion on: 20 Killer JavaScript One-Liners That’ll Save You Hours of Coding 🤯🔥

Collapse
 
wiseai profile image
Mahmoud Harmouch • Edited

Thanks for sharing. I have added the python equivalent (assuming an array is a list in python for simplicity):

1) Find the max value in an array:

max(array)
Enter fullscreen mode Exit fullscreen mode

2) Remove duplicates from an array:

[*set(array)]
Enter fullscreen mode Exit fullscreen mode

3) Generate a random number between 1 and 100:

import random; random.randint(1, 100) # one-liner, LOL.
Enter fullscreen mode Exit fullscreen mode

4) Check if a string is a valid number:

Your method is not correct:

!isNaN(parseFloat("123.asd")) // true
Enter fullscreen mode Exit fullscreen mode

Probably you meant "Check if a string contains a number"?

python equivalent:

string.lstrip('-').replace('.','',1).isdigit()
Enter fullscreen mode Exit fullscreen mode

5) Get the current date and time:

from datetime import datetime; datetime.utcnow() # datetime.datetime(2023, 1, 28, 8, 24, 41, 221834)
Enter fullscreen mode Exit fullscreen mode

6) Check if a variable is an array:

type(array) is list
Enter fullscreen mode Exit fullscreen mode

7) Check if a variable is an object:

issubclass(variable , object)
Enter fullscreen mode Exit fullscreen mode

8) Convert an array to a string:

",".join(array)
Enter fullscreen mode Exit fullscreen mode

9) Check if a variable is a function:

import inspect; inspect.isfunction(variable)
Enter fullscreen mode Exit fullscreen mode

10) Convert an object to an array:

Depends on the type of the object. For example dict:

[*dict_object.items())]
Enter fullscreen mode Exit fullscreen mode

11) Count the occurrences of an element in an array:

array.count(element)
Enter fullscreen mode Exit fullscreen mode

12) Create a new object with a dynamic key and value:

{ key: value}
Enter fullscreen mode Exit fullscreen mode

13) Check if a string is a palindrome:

string == string[::-1]
Enter fullscreen mode Exit fullscreen mode

14) Get the the sum of all the numbers in an array

sum(array)
Enter fullscreen mode Exit fullscreen mode

or

from functools import reduce, operator; reduce(operator.add, array) 
Enter fullscreen mode Exit fullscreen mode

15) Get the current timestamp:

from datetime import datetime; datetime.timestamp(datetime.now())   
Enter fullscreen mode Exit fullscreen mode

16) Check if a variable is null (None in python):

not variable
Enter fullscreen mode Exit fullscreen mode

17) Check if a variable is undefined:

'variable' in vars() or 'variable' in globals()
Enter fullscreen mode Exit fullscreen mode

18) Find the minimum value in an array

min(array)
Enter fullscreen mode Exit fullscreen mode

19) Check if an array is empty:

not array
Enter fullscreen mode Exit fullscreen mode

20) Create a new array with a specified range of numbers:

[* range(n)]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rammcodes profile image
Ram Maheshwari ♾️

This is amazing, Thanks a lot for sharing 🔥🙌

Collapse
 
rjbudzynski profile image
rjbudzynski

19 can be simplified tonot array.

6 to type(array) is list

BTW in a python context array usually refers to numpy array rather than list.

Collapse
 
wiseai profile image
Mahmoud Harmouch

Thanks for the heads up. Fixed!