Skip to content

Lists

toolbox_python.lists 🔗

Summary

The lists module is used to manipulate and enhance Python list's.

Details

Note that functions in this module will only take-in and manipulate existing list objects, and also output list objects. It will not sub-class the base list object, or create new 'list-like' objects. It will always maintain pure python types at it's core.

flatten 🔗

flatten(
    list_of_lists: Union[scalar, collection],
    base_type: Optional[type] = None,
    levels: Optional[int] = None,
) -> any_list

Summary

For a given list of list's, flatten it out to be a single list.

Details

Under the hood, this function will call the more_itertools.collapse() function. The difference between this function and the more_itertools.collapse() function is that the one from more_itertools will return a chain object, not a list object. So, all we do here is call the more_itertools.collapse() function, then parse the result in to a list() function to ensure that the result is always a list object.

Parameters:

Name Type Description Default
list_of_lists list[any_list]

The input list of list's that you'd like to flatten to a single-level list.

required
base_type Optional[type]

Binary and text strings are not considered iterable and will not be collapsed. To avoid collapsing other types, specify base_type.
Defaults to None.

None
levels Optional[int]

Specify levels to stop flattening after a certain nested level.
Defaults to None.

None

Raises:

Type Description
TypeError

If any of the inputs parsed to the parameters of this function are not the correct type. Uses the @typeguard.typechecked decorator.

Returns:

Type Description
any_list

The updated list.

Examples
Set up
1
>>> from toolbox_python.lists import flatten

Example 1: Basic list, same input & output
1
>>> print(flatten([0, 1, 2, 3]))
Terminal
[0, 1, 2, 3]

Conclusion: Successful flattening.

Example 2: List containing two lists
1
>>> print(flatten([[0, 1], [2, 3]]))
Terminal
[0, 1, 2, 3]

Conclusion: Successful flattening.

Example 3: List containing a list and other data
1
>>> print(flatten([0, 1, [2, 3]]))
Terminal
[0, 1, 2, 3]

Conclusion: Successful flattening.

Example 4: List containing two lists and other data
1
>>> print(flatten([[0, 1], [2, 3], 4, 5]))
Terminal
[0, 1, 2, 3, 4, 5]

Conclusion: Successful flattening.

Example 5: List containing a list, a tuple, and other data
1
>>> print(flatten([[0, 1], (2, 3), 4, 5]))
Terminal
[0, 1, 2, 3, 4, 5]

Conclusion: Successful flattening.

Example 6: List containing up to three levels deep
1
>>> print(flatten([[0, 1], [2, 3, [4, 5]]]))
Terminal
[0, 1, 2, 3, 4, 5]

Conclusion: Successful flattening.

Example 7: List containing up to three levels deep, plus other data
1
>>> print(flatten([[0, 1], [2, 3, [4, 5]], 6, 7]))
Terminal
[0, 1, 2, 3, 4, 5, 6, 7]

Conclusion: Successful flattening.

Example 8: List containing up to four levels deep
1
>>> print(flatten([[0, 1], [2, 3, [4, [5]]]]))
Terminal
[0, 1, 2, 3, 4, 5]

Conclusion: Successful flattening.

See Also
Source code in src/toolbox_python/lists.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
@typechecked
def flatten(
    list_of_lists: Union[scalar, collection],
    base_type: Optional[type] = None,
    levels: Optional[int] = None,
) -> any_list:
    """
    !!! note "Summary"
        For a given `#!py list` of `#!py list`'s, flatten it out to be a single `#!py list`.

    ???+ info "Details"
        Under the hood, this function will call the [`#!py more_itertools.collapse()`][more_itertools.collapse] function. The difference between this function and the [`#!py more_itertools.collapse()`][more_itertools.collapse] function is that the one from [`#!py more_itertools`][more_itertools] will return a `chain` object, not a `list` object. So, all we do here is call the [`#!py more_itertools.collapse()`][more_itertools.collapse] function, then parse the result in to a `#!py list()` function to ensure that the result is always a `#!py list` object.

        [more_itertools]: https://more-itertools.readthedocs.io/en/stable/api.html
        [more_itertools.collapse]: https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.collapse

    Params:
        list_of_lists (list[any_list]):
            The input `#!py list` of `#!py list`'s that you'd like to flatten to a single-level `#!py list`.
        base_type (Optional[type], optional):
            Binary and text strings are not considered iterable and will not be collapsed. To avoid collapsing other types, specify `base_type`.<br>
            Defaults to `#!py None`.
        levels (Optional[int], optional):
            Specify `levels` to stop flattening after a certain nested level.<br>
            Defaults to `#!py None`.

    Raises:
        TypeError: If any of the inputs parsed to the parameters of this function are not the correct type. Uses the [`@typeguard.typechecked`](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.typechecked) decorator.

    Returns:
        (any_list):
            The updated `#!py list`.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> from toolbox_python.lists import flatten
        ```

        ```{.py .python linenums="1" title="Example 1: Basic list, same input & output"}
        >>> print(flatten([0, 1, 2, 3]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 2: List containing two lists"}
        >>> print(flatten([[0, 1], [2, 3]]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 3: List containing a list and other data"}
        >>> print(flatten([0, 1, [2, 3]]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 4: List containing two lists and other data"}
        >>> print(flatten([[0, 1], [2, 3], 4, 5]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3, 4, 5]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 5: List containing a list, a tuple, and other data"}
        >>> print(flatten([[0, 1], (2, 3), 4, 5]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3, 4, 5]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 6: List containing up to three levels deep"}
        >>> print(flatten([[0, 1], [2, 3, [4, 5]]]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3, 4, 5]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 7: List containing up to three levels deep, plus other data"}
        >>> print(flatten([[0, 1], [2, 3, [4, 5]], 6, 7]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3, 4, 5, 6, 7]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 8: List containing up to four levels deep"}
        >>> print(flatten([[0, 1], [2, 3, [4, [5]]]]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3, 4, 5]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

    ??? tip "See Also"
        - [`more_itertools`](https://more-itertools.readthedocs.io/en/stable/api.html)
        - [`more_itertools.collapse()`](https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.collapse)
    """
    return list(
        itertools_collapse(
            iterable=list_of_lists,  # type: ignore
            base_type=base_type,
            levels=levels,
        )
    )

flat_list 🔗

flat_list(*inputs: Any) -> any_list

Summary

Take in any number of inputs, and output them all in to a single flat list.

Parameters:

Name Type Description Default
inputs Any

Any input.

()

Raises:

Type Description
TypeError

If any of the inputs parsed to the parameters of this function are not the correct type. Uses the @typeguard.typechecked decorator.

Returns:

Type Description
any_list

The input having been coerced to a single flat list.

Examples
Set up
1
>>> from toolbox_python.lists import flat_list

Example 1: Basic input & output
1
>>> print(flat_list(0, 1, 2, 3))
Terminal
[0, 1, 2, 3]

Conclusion: Successful flattening.

Example 2: Multiple lists
1
>>> print(flat_list([0, 1], [2, 3]))
Terminal
[0, 1, 2, 3]

Conclusion: Successful flattening.

Example 3: List and other data
1
>>> print(flat_list(0, 1, [2, 3]))
Terminal
[0, 1, 2, 3]

Conclusion: Successful flattening.

Example 4: Multiple lists and other data
1
>>> print(flat_list([0, 1], [2, 3], 4, 5))
Terminal
[0, 1, 2, 3, 4, 5]

Conclusion: Successful flattening.

Example 5: List and a tuple and other data
1
>>> print(flat_list([0, 1], (2, 3), 4, 5))
Terminal
[0, 1, 2, 3, 4, 5]

Conclusion: Successful flattening.

Example 6: List and a nested list
1
>>> print(flat_list([0, 1], [2, 3, [4, 5]]))
Terminal
[0, 1, 2, 3, 4, 5]

Conclusion: Successful flattening.

Example 7: List and a nested list and other data
1
>>> print(flat_list([0, 1], [2, 3, [4, 5]], 6, 7))
Terminal
[0, 1, 2, 3, 4, 5, 6, 7]

Conclusion: Successful flattening.

Example 8: Deep nested lists
1
>>> print(flat_list([0, 1], [2, 3, [4, [5]]]))
Terminal
[0, 1, 2, 3, 4, 5]

Conclusion: Successful flattening.

See Also
Source code in src/toolbox_python/lists.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
@typechecked
def flat_list(*inputs: Any) -> any_list:
    """
    !!! note "Summary"
        Take in any number of inputs, and output them all in to a single flat `#!py list`.

    Params:
        inputs (Any):
            Any input.

    Raises:
        TypeError: If any of the inputs parsed to the parameters of this function are not the correct type. Uses the [`@typeguard.typechecked`](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.typechecked) decorator.

    Returns:
        (any_list):
            The input having been coerced to a single flat `#!py list`.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> from toolbox_python.lists import flat_list
        ```

        ```{.py .python linenums="1" title="Example 1: Basic input & output"}
        >>> print(flat_list(0, 1, 2, 3))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 2: Multiple lists"}
        >>> print(flat_list([0, 1], [2, 3]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 3: List and other data"}
        >>> print(flat_list(0, 1, [2, 3]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 4: Multiple lists and other data"}
        >>> print(flat_list([0, 1], [2, 3], 4, 5))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3, 4, 5]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 5: List and a tuple and other data"}
        >>> print(flat_list([0, 1], (2, 3), 4, 5))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3, 4, 5]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 6: List and a nested list"}
        >>> print(flat_list([0, 1], [2, 3, [4, 5]]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3, 4, 5]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 7: List and a nested list and other data"}
        >>> print(flat_list([0, 1], [2, 3, [4, 5]], 6, 7))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3, 4, 5, 6, 7]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

        ```{.py .python linenums="1" title="Example 8: Deep nested lists"}
        >>> print(flat_list([0, 1], [2, 3, [4, [5]]]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [0, 1, 2, 3, 4, 5]
        ```
        !!! success "Conclusion: Successful flattening."
        </div>

    ??? tip "See Also"
        - [`flatten()`][toolbox_python.lists.flatten]
    """
    return flatten(list(inputs))

product 🔗

product(*iterables) -> list[any_tuple]

Summary

For a given number of iterables, perform a cartesian product on them, and return the result as a list.

Details

Under the hood, this function will call the itertools.product() function. The difference between this function and the itertools.product() function is that the one from itertools will return a product object, not a list object. So, all we do here is call the itertools.product() function, then parse the result in to a list() function to ensure that the result is always a list object.

Parameters:

Name Type Description Default
iterables Any

The input iterables that you'd like to expand out.

()

Returns:

Type Description
list[tuple[Any, ...]]

The updated list list of tuple's representing the Cartesian product of the provided iterables.

Examples
Set up
1
>>> from toolbox_python.lists import product

Example 1: Basic input & output
1
>>> print(product([1], [11], [111]))
Terminal
[
    (1, 11, 111),
]

Conclusion: Successful conversion.

Example 2: Multiple lists
1
>>> print(product([1, 2], [11], [111]))
Terminal
[
    (1, 11, 111),
    (2, 11, 111),
]

Conclusion: Successful conversion.

Example 3: List and other data
1
>>> print(product([1, 2], [11], [111, 222]))
Terminal
[
    (1, 11, 111),
    (1, 11, 222),
    (2, 11, 111),
    (2, 11, 222),
]

Conclusion: Successful conversion.

Example 4: Multiple lists and other data
1
>>> print(product([1, 2], [11, 22], [111, 222]))
Terminal
[
    (1, 11, 111),
    (1, 11, 222),
    (1, 22, 111),
    (1, 22, 222),
    (2, 11, 111),
    (2, 11, 222),
    (2, 22, 111),
    (2, 22, 222),
]

Conclusion: Successful conversion.

See Also
Source code in src/toolbox_python/lists.py
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
def product(*iterables) -> list[any_tuple]:
    """
    !!! note "Summary"
        For a given number of `#!py iterables`, perform a cartesian product on them, and return the result as a list.

    ???+ info "Details"
        Under the hood, this function will call the [`#!py itertools.product()`][itertools.product] function. The difference between this function and the [`#!py itertools.product()`][itertools.product] function is that the one from [`#!py itertools`][itertools] will return a `product` object, not a `list` object. So, all we do here is call the [`#!py itertools.product()`][itertools.product] function, then parse the result in to a `#!py list()` function to ensure that the result is always a `#!py list` object.

        [itertools]: https://docs.python.org/3/library/itertools.html
        [itertools.product]: https://docs.python.org/3/library/itertools.html#itertools.product

    Params:
        iterables (Any):
            The input `#!py iterables` that you'd like to expand out.

    Returns:
        (list[tuple[Any, ...]]):
            The updated `#!py list` list of `#!py tuple`'s representing the Cartesian product of the provided iterables.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> from toolbox_python.lists import product
        ```

        ```{.py .python linenums="1" title="Example 1: Basic input & output"}
        >>> print(product([1], [11], [111]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [
            (1, 11, 111),
        ]
        ```
        !!! success "Conclusion: Successful conversion."
        </div>

        ```{.py .python linenums="1" title="Example 2: Multiple lists"}
        >>> print(product([1, 2], [11], [111]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [
            (1, 11, 111),
            (2, 11, 111),
        ]
        ```
        !!! success "Conclusion: Successful conversion."
        </div>

        ```{.py .python linenums="1" title="Example 3: List and other data"}
        >>> print(product([1, 2], [11], [111, 222]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [
            (1, 11, 111),
            (1, 11, 222),
            (2, 11, 111),
            (2, 11, 222),
        ]
        ```
        !!! success "Conclusion: Successful conversion."
        </div>

        ```{.py .python linenums="1" title="Example 4: Multiple lists and other data"}
        >>> print(product([1, 2], [11, 22], [111, 222]))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        [
            (1, 11, 111),
            (1, 11, 222),
            (1, 22, 111),
            (1, 22, 222),
            (2, 11, 111),
            (2, 11, 222),
            (2, 22, 111),
            (2, 22, 222),
        ]
        ```
        !!! success "Conclusion: Successful conversion."
        </div>

    ??? tip "See Also"
        - [itertools](https://docs.python.org/3/library/itertools.html)
        - [itertools.product()](https://docs.python.org/3/library/itertools.html#itertools.product)
    """
    return list(itertools_product(*iterables))