Skip to content

Python ellipsis

  • by

In Python, the ellipsis (…) is a special object called “ellipsis” or “ellipsis literal.” It is represented by three consecutive dots and is used as a placeholder or sentinel value in various contexts. The ellipsis is also sometimes referred to as a “triple-dot operator” or “suspension points“.

Slice Notation: The ellipsis is commonly used in slice notation to represent a full slice along a particular axis. It allows you to select all elements along multiple dimensions in a multi-dimensional array or sequence.

my_list = [1, 2, 3, 4, 5]
print(my_list[1:...])  # This selects all elements from index 1 to the end of the list.

Function Definitions: In function definitions, the ellipsis is used as a placeholder for unimplemented functionality, indicating that the function’s implementation is not yet complete. It is commonly seen as a convention in the docstrings of functions to signify that certain parts need to be filled in later.

def my_function(arg1, arg2, ...):
    This function is not yet implemented. It will do something with arg1 and arg2.

Type Annotations (Python 3.5+): In type hinting and annotations, you can use the ellipsis to represent unspecified or variable arguments in the function signature. It is used to indicate that additional parameters are allowed or that the specific type is not yet determined.

def my_function(arg1: str, arg2: ...) -> ...:
    # Function implementation here

NumPy Library: The ellipsis is also used in the NumPy library to index multi-dimensional arrays along all dimensions. It is often used in complex array indexing operations.

import numpy as np

my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(my_array[..., 1])  # This selects the second column [2, 5, 8] from the array.

Python ellipsis example

Here are a few examples of how the ellipsis is used in Python:

Slice Notation:

my_list = [1, 2, 3, 4, 5]
print(my_list[1:...])  # Output: [2, 3, 4, 5]

Function Definitions:

def add_numbers(a, b, ...):
    This function adds two or more numbers.
    result = a + b
    for num in args:
        result += num
    return result

sum_result = add_numbers(1, 2, 3, 4)
print(sum_result)  # Output: 10

Type Annotations (Python 3.5+):

def process_data(data: list[int, ...]) -> tuple[bool, ...]:
    This function processes a list of integers and returns a tuple of booleans.
    # Some data processing code here
    return True, False, ...

result = process_data([1, 2, 3, 4, 5])
print(result)  # Output: (True, False, Ellipsis)

NumPy Library:

import numpy as np

my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(my_array[..., 1])  # Output: [2, 5, 8]


Python ellipsis

In these examples, the ellipsis is used to represent full slices, placeholders in function definitions, unspecified types in type annotations, and multi-dimensional array indexing along all dimensions.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *