#### Previous topic

67. webgl — View and manipulate 3D models in your browser.

69. mydict

# 68. olist — Some convenient shortcuts for common list operations.¶

While most of these functions look (and work) like set operations, their result differs from using Python builtin Sets in that they preserve the order of the items in the lists.

Classes defined in module olist

Functions defined in module olist

olist.roll(a, n=1)

Roll the elements of a list n positions forward (backward if n < 0)

olist.union(a, b)

Return a list with all items in a or in b, in the order of a,b.

olist.difference(a, b)

Return a list with all items in a but not in b, in the order of a.

olist.symdifference(a, b)

Return a list with all items in a or b but not in both.

olist.intersection(a, b)

Return a list with all items in a and in b, in the order of a.

olist.concatenate(a)

Concatenate a list of lists

olist.flatten(a, recurse=False)

Flatten a nested list.

By default, lists are flattened one level deep. If recurse=True, flattening recurses through all sublists.

```>>> flatten([[[3.,2,],6.5,],[5],6,'hi'])
[[3.0, 2], 6.5, 5, 6, 'hi']
>>> flatten([[[3.,2,],6.5,],[5],6,'hi'],True)
[3.0, 2, 6.5, 5, 6, 'hi']
```
olist.select(a, b)

Return a subset of items from a list.

Returns a list with the items of a for which the index is in b.

olist.remove(a, b)

Returns the complement of select(a,b).

olist.toFront(l, i)

Add or move i to the front of list l

l is a list. If i is in the list, it is moved to the front of the list. Else i is added at the front of the list.

This changes the list inplace and does not return a value.

olist.collectOnLength(items, return_indices=False)

Collect items of a list in separate bins according to the item length.

items is a list of items of any type having the len() method. The items are put in separate lists according to their length.

The return value is a dict where the keys are item lengths and the values are lists of items with this length.

If return_indices is True, a second dict is returned, with the same keys, holding the original indices of the items in the lists.