Count Occurrences in a List from Same Turtle?
In this article
The direct answer is: to count how many times a value appears in a list stored in a single Turtle file, you query the list with SPARQL and use COUNT with FILTER or GROUP BY on the list elements. In practice, most people mean counting occurrences of a specific item (like a class, property, or literal) across all triples in that Turtle file, which is done with a simple SELECT (COUNT(?s) AS ?count) WHERE { ?s ?p ?o } query.
Background: What Turtle Lists Are and Why Counting Is Tricky
Turtle (Terse RDF Triple Language) is a serialization format for RDF data. An RDF list is not a native array; it is a linked structure built from blank nodes and the properties rdf:first and rdf:rest. For example, the list (1 2 3) is represented as:
_:b0 rdf:first 1 ; rdf:rest _:b1 . _:b1 rdf:first 2 ; rdf:rest _:b2 . _:b2 rdf:first 3 ; rdf:rest rdf:nil .
Because of this, counting occurrences of an item in a list requires traversing the chain. If you simply count triples with rdf:first, you get the total number of elements across all lists, not per list. To count per list, you need to group by the head node or use a property path.
How to Count Occurrences in a Turtle File
There are two common scenarios:
- Counting a specific value across all triples (e.g., how many times the literal "red" appears as an object). Use:
SELECT (COUNT(?o) AS ?count) WHERE { ?s ?p ?o . FILTER(?o = "red") }. - Counting elements in a specific list (e.g., the list attached to
:myList). Use a SPARQL property path:SELECT (COUNT(?item) AS ?count) WHERE { :myList rdf:rest*/rdf:first ?item }.
For counting occurrences of each distinct value in a list, use GROUP BY:
SELECT ?item (COUNT(?item) AS ?count)
WHERE { :myList rdf:rest*/rdf:first ?item }
GROUP BY ?itemThis returns each unique element and how many times it appears in that list.
Choosing the Right Query for Your Data
Your exact query depends on what you mean by "same turtle" (i.e., the same file) and what you are counting:
- If you want the total number of triples in the file, use
SELECT (COUNT(*) AS ?count) WHERE { ?s ?p ?o }. - If you want to count occurrences of a property (e.g.,
foaf:name), useSELECT (COUNT(?s) AS ?count) WHERE { ?s foaf:name ?o }. - If you want to count list elements across all lists in the file, use
SELECT (COUNT(?item) AS ?count) WHERE { ?list rdf:rest*/rdf:first ?item }.
Be aware that blank nodes are scoped to the file, so if you merge multiple Turtle files, blank node IDs may clash, but that is a separate issue.
Common Problems and Misconceptions
One frequent mistake is assuming that rdf:first appears once per element, which is true, but counting rdf:first triples alone gives the total number of elements in all lists, not per list. Another issue is that COUNT(DISTINCT ?item) counts unique values, not occurrences. Many users also forget that literals with different datatypes (e.g., "1"^^xsd:integer vs "1"^^xsd:string) are different values. Also, if the list contains blank nodes, counting them requires deciding whether to count the blank node or the value it represents.
Comparing Turtle Lists to Other RDF Structures
RDF also supports sets (rdf:Bag) and sequences (rdf:Seq), but they are not ordered and use different properties (rdf:li). Counting elements in a Bag is simpler because each element is a separate triple with rdf:li. However, lists are more common for ordered data. If you need to count occurrences frequently, consider storing data as repeated triples rather than a list, because querying becomes trivial: SELECT ?item (COUNT(?item) AS ?count) WHERE { ?s :hasItem ?item } GROUP BY ?item.
Practical Tools and Workflows
You can run SPARQL queries on Turtle files using tools like Apache Jena's arq command line, RDFLib in Python, or online SPARQL endpoints. For a quick count without writing code, use a text editor to count occurrences of a literal string, but that is error-prone because it may match substrings. For example, counting "red" with a text tool would also match "red"@en or "red"^^xsd:string, which are different values. A proper SPARQL query avoids that.
Safety and Accuracy Notes
When counting, always check the datatype and language tags of literals. For instance, "apple" and "apple"@en are distinct. Also, be aware that Turtle files can use prefixes and base IRIs; a query must resolve these correctly. If you are counting blank nodes, remember that their IDs are only meaningful within the file. Finally, for very large files, counting in a streaming fashion may be more memory-efficient, but for typical Turtle files, a SPARQL query is fine.
Frequently Asked Questions
How do I count how many times a specific value appears in a list?
Use the property path query: SELECT (COUNT(?item) AS ?count) WHERE { :myList rdf:rest*/rdf:first ?item . FILTER(?item = "target") }. Replace :myList with your list's IRI and "target" with the value.
Can I count occurrences without SPARQL?
Yes, you can parse the Turtle file with a library like RDFLib and iterate through the triples, but SPARQL is more concise and less error-prone, especially for nested lists.
What if the list is nested inside another list?
Nested lists still use the same rdf:first/rdf:rest structure. The property path rdf:rest*/rdf:first will traverse all levels, so it counts all elements, including those in sublists. If you need to count only top-level elements, you must filter out sublists, which is more complex.
Does the order of elements matter for counting?
No, counting occurrences is order-independent. If you need to count positions, you would need to track the index, which is not directly supported in SPARQL without additional extensions.
Why does my count include blank nodes I didn't expect?
Blank nodes are valid RDF values. If your list contains blank nodes, they will be counted as items. To count only literals or IRIs, add a FILTER(isLiteral(?item) || isIRI(?item)).