Find distinct values (Python)
Finds the distinct values of a column for rows in a table.
This method finds all rows that match the filter, or all rows if no filter is applied. There can be performance, latency, and billing implications if there are many matching rows.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Returns a list of the distinct values of the specified key.
Parameters
Use the distinct method, which belongs to the astrapy.Table class.
Method signature
distinct(
key: str,
*,
filter: Dict[str, Any],
general_method_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
) -> list[Any]
| Name | Type | Summary |
|---|---|---|
|
|
The key for which to find values. This can be:
See the examples for usage. |
|
|
Optional. An object that defines filter criteria using the Data API filter syntax. Only rows that match this filter criteria will be inspected. For a list of available filter operators and more examples, see Filter operators for tables (Python). Default: No filter |
|
|
Optional. The maximum time, in milliseconds, that the whole operation, which might involve multiple HTTP requests, can take. This parameter is aliased as Default: The default value for the table.
This default is 30 seconds unless you specified a different default when you initialized the |
|
|
Optional. The maximum time, in milliseconds, that the client should wait for each underlying HTTP request. Default: The default value for the table.
This default is 30 seconds unless you specified a different default when you initialized the |
Examples
The following examples demonstrate how to find distinct values of a column for rows in a table.
Find distinct values of a column for rows that match a filter
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Find distinct values
result = table.distinct(
"publication_year",
filter={
"$and": [
{"is_checked_out": False},
{"number_of_pages": {"$lt": 300}},
]
},
)
print(result)
Find distinct values of a column for all rows
To find the distinct values of a column for all rows in the table, use an empty filter.
You should avoid this if you have a large number of rows.
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Find distinct values
result = table.distinct("publication_year", filter={})
print(result)
Find distinct values of an entry in a map column
To find distinct values for an entry in a map column, use dot notation.
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Find distinct values
result = table.distinct(
"metadata.language",
filter={
"$and": [
{"is_checked_out": False},
{"number_of_pages": {"$lt": 300}},
]
},
)
print(result)
Find distinct values of an index in a list column
To find distinct values for an index in a list column, use dot notation with the desired index.
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
table = database.get_table("TABLE_NAME")
# Find distinct values
result = table.distinct(
"topics.2",
filter={
"$and": [
{"is_checked_out": False},
{"number_of_pages": {"$lt": 300}},
]
},
)
print(result)
Client reference
For more information, see the client reference.