diff --git a/pyopencl/algorithm.py b/pyopencl/algorithm.py
index 0d6b9f58da3c68c0d6e6d902015e1885aa6369a7..73e43d561aa682e1e807259c6974fad862c0e319 100644
--- a/pyopencl/algorithm.py
+++ b/pyopencl/algorithm.py
@@ -864,7 +864,7 @@ class ListOfListsBuilder:
             __global ${index_t} *num_non_empty_list
         """
         from sys import version_info
-        if (version_info > (3, 0)):
+        if version_info > (3, 0):
             arguments = Template(arguments)
         else:
             arguments = Template(arguments, disable_unicode=True)
@@ -876,7 +876,7 @@ class ListOfListsBuilder:
                 input_expr="count[i] == 0 ? 0 : 1",
                 scan_expr="a+b", neutral="0",
                 output_statement="""
-                    compressed_indices[i + 1] = item;
+                    if (i + 1 < N) compressed_indices[i + 1] = item;
                     if (prev_item != item) {
                         nonempty_indices[item - 1] = i;
                         compressed_counts[item - 1] = count[i];