List¶
Utilities for lists.
- disseminate.utils.list.chunks(lst, N)¶
Return a generator in chunks of N.
- Parameters
- lstUnion[List, Tuple]
The iterable to break into chunks.
- Nint
The size of chunks.
- Returns
- Generator
A generator with sublists of items from ‘l’ broken into chunks of ‘N’.
- disseminate.utils.list.dupes(lst, key=None)¶
Find duplicates in a list using, optionally, a key mapping function.
- Parameters
- lstlist
The list to check for duplicates.
- keyOptional[Callable]
A mapping function that takes a list item and returns an alternate value to compare for duplicates.
Examples
>>> dupes([1, 2, 2, 3, 3, 3, 4, 5, 6, 6, 7]) [2, 2, 3, 3, 3, 6, 6] >>> dupes([(1, 'a'), (2, 'a'), (3, 'b'), (4, 'c')], key=lambda i:i[1]) [(1, 'a'), (2, 'a')] >>> dupes([(1, 'a'), (2, 'a'), (3, 'b'), (4, 'c')], key=lambda i:i[0]) []
- class disseminate.utils.list.filterfalse¶
Bases:
object
filterfalse(function or None, sequence) –> filterfalse object
Return those items of sequence for which function(item) is false. If function is None, return the items that are false.
- disseminate.utils.list.flatten(lst)¶
Flatten a list of lists.
Examples
>>> list(flatten([1, 2, 3])) [1, 2, 3] >>> list(flatten([[1, 2, 3], [4, 5, 6]])) [1, 2, 3, 4, 5, 6] >>> list(flatten([[1,2], [3, [4, 5], 6]])) [1, 2, 3, 4, 5, 6]
- disseminate.utils.list.md5hash(lst)¶
Generates a hash of a list or tuple.
Examples
>>> md5hash(('a', 'b', 1)) '68b6a776378decbb4a79cda89087c4ce' >>> md5hash(('a', 'b', '1')) '68b6a776378decbb4a79cda89087c4ce' >>> md5hash(['a', [1, 'b'], 'c']) '1c81219649292a2ef9240fc997353078' >>> md5hash(['a', ['1', 'b'], 'c']) '1c81219649292a2ef9240fc997353078'
- disseminate.utils.list.transpose(lst)¶
Transpose a list of lists
Examples
>>> transpose([[1, 2, 3], [4, 5, 6]]) [[1, 4], [2, 5], [3, 6]] >>> transpose([[1, 2, 3], [4, 5]]) # Discard mismatched list entries [[1, 4], [2, 5]] >>> transpose([[1, 2], [4, 5, 6]]) # Discard mismatched list entries [[1, 4], [2, 5]]
- disseminate.utils.list.uniq(lst, key=None)¶
Find the unique items in the given list while preserving the order of the list and optionally using a key mapping function.
- Parameters
- lstlist
The list to check for duplicates.
- keyOptional[Callable]
A mapping function that takes a list item and returns an alternate value to compare for unique entries.
Examples
>>> uniq([1, 2, 2, 3, 3, 3, 4, 5, 6, 6, 7]) [1, 2, 3, 4, 5, 6, 7] >>> uniq([(1, 'a'), (2, 'a'), (3, 'b'), (4, 'c')], key=lambda i:i[1]) [(1, 'a'), (3, 'b'), (4, 'c')] >>> uniq([(1, 'a'), (2, 'a'), (3, 'b'), (4, 'c')], key=lambda i:i[0]) [(1, 'a'), (2, 'a'), (3, 'b'), (4, 'c')]
- disseminate.utils.list.unique_everseen(iterable, key=None)¶
List unique elements, preserving order. Remember all elements ever seen.
- disseminate.utils.list.unwrap(lst)¶
Unwrap lists with only a single item.
- Parameters
- lstlist
The list to unwrap
- Returns
- unwrapped_listlist
The unwrapped list
Examples
>>> unwrap([1, 2, 3]) [1, 2, 3] >>> unwrap([1]) 1 >>> unwrap([[1]]) 1 >>> unwrap([[[3]]]) 3