JavaScript's "..." in Python? 😰

Rajesh Joshi - Jan 4 '22 - - Dev Community

Spread Operator in JavaScript is one of the best thing given by GOD 😝

What is Spread Operator in JS?

Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 values are expected. It allows us the privilege to obtain a list of parameters from an array.

let arr1 = [1, 2, 3];
let arr2 = [4, 5];

// Spread Operator
let arr3 = [...arr1, ...arr2];
console.log(arr3); // [ 1, 2, 3, 4, 5 ]
Enter fullscreen mode Exit fullscreen mode

What is ... in Python?

... or Ellipsis is a Python Object. It has no Methods. It is a singleton Object i.e, provides easy access to single instances.

# style1
def foo():
    pass

# style2
def foo():
    ...
# both the styles are same
Enter fullscreen mode Exit fullscreen mode

More example

Use an Ellipsis in Numpy To Omit Dimensions

import numpy as np

A = np.random.rand(2, 3, 2)
print(A)
# [[[1.83948755e-01 7.34415667e-04]
#   [7.34273680e-01 3.92355559e-01]
#   [6.73369042e-01 2.93722630e-01]]
#
#  [[8.17789457e-01 1.13353920e-01]
#   [3.53150493e-01 2.70330813e-01]
#   [7.46381962e-01 7.42168707e-01]]]

print(A[:, :, 1])
# [[6.75415667e-04 4.58155559e-01 9.02622630e-01]
#  [9.13353920e-01 4.70330813e-01 6.42168707e-01]]

print(A[..., 1])
# [[6.75415667e-04 4.58155559e-01 9.02622630e-01]
# [9.13353920e-01 4.70330813e-01 6.42168707e-01]]

print(A[Ellipsis, 1])
# [[6.75415667e-04 4.58155559e-01 9.02622630e-01]
#  [9.13353920e-01 4.70330813e-01 6.42168707e-01]]
Enter fullscreen mode Exit fullscreen mode

Conclusion

You can use ... or Ellipsis in place of pass in python. or use in numpy To omit dimensions.


Hurray! You just learned What is the difference between JS Spread Operator and ... Python.


I hope, you guys liked this quick tutorial. If so, then please don't forget to drop a Like ❀️

And also, help me reach 1k Subscribers 🀩, on my YouTube channel.

Happy Coding! πŸ˜ƒπŸ’»

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .