Skip to content

nayakrujul/nested-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nested-tools

The nested_tools library in Python

Setup

From PIP

$ pip install nestedtools

Usage

Import library

import nested_tools as nt

Find a nested index from the item

l = [['foo', 'bar'], 'baz']
print(nt.index(l, 'bar'))

Output: (0, 1)

Get the item from a nested index

print(nt.get(l, (0, 1)))

Output: bar

Flatten a nested list

print(list(nt.flatten(l)))

Output: ['foo', 'bar', 'baz']

Transpose a flat list into a nested shape

old_list = ['A', 'B', 'C', 'D', 'E', 'F']
new_shape = [[0], [1, 2], [3, [4, 5]]]
print(list(nt.transpose(old_list, new_shape)))

Output: [['A'], ['B', 'C'], ['D', ['E', 'F']]]

Get the shape list from a nested list

l = [['A'], ['B', 'C'], ['D', ['E', 'F']]]
print(list(nt.to_shape(l)))

Output: [[0], [1, 2], [3, [4, 5]]]

Make one nested list the same shape as another

a = [['foo', 'bar'], 'baz']
b = ['A', ['B', ['C']]]
print(list(nt.make_same_shape(a, b)))

Output: ['foo', ['bar', ['baz']]]

Convert all iterables to list

l = [{1, 2, (3,)}, [4, {5}, (6,)]]
print(list(nt.convert_all(l)))

Output: [[1, 2, [3]], [4, [5], [6]]]

convert_all takes two more optional parameters

  • to - a type to convert to (default = list):
print(tuple(nt.convert_all(l, tuple)))

Output: ((1, 2, (3,)), (4, (5,), (6,)))

  • exclude - a tuple of any iterable types to not convert (default = (str,)):
print(list(nt.convert_all(l, exclude=(set,))))

Output: [{1, 2, (3,)}, [4, {5}, [6]]]

About

The nested_tools library in Python

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages