pwnlib.util.lists — Operations on lists

pwnlib.util.lists.concat(l) list[source]

Concats a list of lists into a list.

Example

>>> concat([[1, 2], [3]])
[1, 2, 3]
pwnlib.util.lists.concat_all(*args) list[source]

Concats all the arguments together.

Example

>>> concat_all(0, [1, (2, 3)], [([[4, 5, 6]])])
[0, 1, 2, 3, 4, 5, 6]
pwnlib.util.lists.findall(l, e) l[source]

Generate all indices of needle in haystack, using the Knuth-Morris-Pratt algorithm.

Example

>>> foo = findall([1,2,3,4,4,3,4,2,1], 4)
>>> next(foo)
3
>>> next(foo)
4
>>> next(foo)
6
>>> list(foo) # no more appearances
[]
>>> list(findall("aaabaaabc", "aab"))
[1, 5]
pwnlib.util.lists.group(n, lst, underfull_action='ignore', fill_value=None) list[source]

Split sequence into subsequences of given size. If the values cannot be evenly distributed among into groups, then the last group will either be returned as is, thrown out or padded with the value specified in fill_value.

Parameters
  • n (int) – The size of resulting groups

  • lst – The list, tuple or string to group

  • underfull_action (str) – The action to take in case of an underfull group at the end. Possible values are ‘ignore’, ‘drop’ or ‘fill’.

  • fill_value – The value to fill into an underfull remaining group.

Returns

A list containing the grouped values.

Example

>>> group(3, "ABCDEFG")
['ABC', 'DEF', 'G']
>>> group(3, 'ABCDEFG', 'drop')
['ABC', 'DEF']
>>> group(3, 'ABCDEFG', 'fill', 'Z')
['ABC', 'DEF', 'GZZ']
>>> group(3, list('ABCDEFG'), 'fill')
[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', None, None]]
>>> group(2, tuple('1234'), 'fill')
[('1', '2'), ('3', '4')]
pwnlib.util.lists.ordlist(s) list[source]

Turns a string into a list of the corresponding ascii values.

Example

>>> ordlist("hello")
[104, 101, 108, 108, 111]
pwnlib.util.lists.partition(lst, f, save_keys=False) list[source]

Partitions an iterable into sublists using a function to specify which group they belong to.

It works by calling f on every element and saving the results into an collections.OrderedDict.

Parameters
  • lst – The iterable to partition

  • f (function) – The function to use as the partitioner.

  • save_keys (bool) – Set this to True, if you want the OrderedDict returned instead of just the values

Example

>>> partition([1,2,3,4,5], lambda x: x&1)
[[1, 3, 5], [2, 4]]
>>> partition([1,2,3,4,5], lambda x: x%3, save_keys=True)
OrderedDict([(1, [1, 4]), (2, [2, 5]), (0, [3])])
pwnlib.util.lists.unordlist(cs) str[source]

Takes a list of ascii values and returns the corresponding string.

Example

>>> unordlist([104, 101, 108, 108, 111])
'hello'