What are the Uses of Ellipsis in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

What are the Uses of Ellipses in Python?

In Python, the Ellipsis or Ellipsis literal ... (three dots) is a built-in singleton object representing an infinite or unspecified number of arguments. It is a constant and does not have any associated methods. You can use this object in your code without importing it simply by using the ... or Ellipsis term. Both of these expressions evaluate the same Ellipsis object in Python.

The above code examples show the evaluation of Ellipsis or Ellipsis literal.

The image below illustrates the output of both print statements. As you can see, Ellipsis literal (...) evaluates to an Ellipsis, which means both are similar and can be used interchangeably.

Uses of Ellipses in Python

The python documentation states that Ellipsis literal or the Ellipsis object is a unique value used primarily in conjunction with extended slicing syntax for data types container, which is user-defined.

Python Ellipses is a singleton constant without particular intended use cases, but it is used with multidimensional data arrays to make handling them more accessible. The Python Ellipsis object appears unnoticeable, but using it properly can make our lives more effortless.

Following are various use cases of Python Ellipses (...) :-

  • It can be used as a placeholder for unwritten code.
  • Slicing multidimensional Data Arrays with NumPy
  • Ellipsis in type hinting.
  • Indexing multidimensional arrays
  • Placeholder in function definitions

You can replace the pass keyword with Ellipsis literal (...) in Python. You can use Ellipsis literal (...) as a "no operation" placeholder for your code inside the function or class, which is not written as of now. The Ellipsis literal doesn't do anything.

Above, you can see two functions, first_func, and second_func, and the first contains an Ellipsis literal, and the second contains a pass keyword. But both don't do anything alone but indicate that both functions must be written.

Meaning of Python Ellipses in Python Type Hints

Another use of an Python Ellipses singleton object was introduced in Python 3.5 or above. Type hinting allows us to declare and use the types of variables, parameters, and return values. Generally, Ellipsis in type hinting provides only part of the type.

Python Ellipses are used in specifying type hints using the typing module, such as Callable[…, str], or it can be used as the Tuple[int, ...] to help type hinting.

Type hinting is a great way to specify the data types you expect in your code. For example, you may want a tuple with the same data type, which means only integers, but the total number of integers can be arbitrary. That’s when the Ellipsis object can come in convenient:

Using Python Ellipses literal (...) within a tuple type hint indicates that you expect all items to be the same type in the Tuple.

Ellipsis for Slicing in Numpy

Numpy is the most valuable and convenient library for data science. It is used to create and handle multidimensional arrays. Using Ellipsis helps in handling multidimensional arrays in NumPy.

Slicing in Numpy refers to extracting portions of elements for an array based on their indexes. You can also perform the slicing on a multidimensional array in NumPy.

Suppose we have an array with 343 * 4 dimensions, and we want to extract the element from the first index from each row.

Here's how you can do it with Ellipsis and normal slice notation:-

Above, we have an array with 343*4 dimensions, and we're extracting elements from the first index using the Ellipsis literal, Slice notation, and Ellipsis method.

Below you can see the output of the bottom of all three print statements is similar because all three ways of extracting elements are correct.

Ellipsis for Slicing in Numpy

Can Three Dots Always be Considered Ellipses?

After learning about the Python Ellipses, you indeed pay attention to the appearance of every Ellipsis literal (...) in Python. Still, you might see three dots that don't denote the Ellipsis object or constant.

When you define a function or for loops within the Python interactive shell, you can see the three dots that continue over multiple lines. Still, it doesn't denote the Ellipsis, but rather it indicates the secondary prompt:

Conclusion

  • Python Ellipsis is a built-in singleton object with no methods.
  • You can use the Python Ellipses object with the Ellipsis term or the Ellipsis literal (...).
  • The Ellipsis also uses as a placeholder in place of the pass keyword.
  • Python Ellipsis object is a unique value used primarily in conjunction with extended slicing syntax for data types container which is user-defined.
  • Ellipsis is primarily used with Numpy for slicing.