Skip to main content

Adds some useful methods on top of Python lists

Project description

Better Python Lists

Makes lists in Python better, by adding some useful methods.

  • List.compact() - removes all None values from the List.
  • List.flatten() - flattens an arbitrarily nested List.

Installation

pip install better-python-lists

Basic Usage

List([1, None, 2, 3, 4, None, None, 5]).compact()
# => List[(1, 2, 3, 4, 5)]

List([["a"], "b", 5, [6, 7, [8, 9, [10]]]]).flatten()
# => List(["a", "b", 5, 6, 7, 8, 9, 10])

Details

Compact

The compact method is inspired by Ruby's Array.compact. It removes all None elements from the List, returning a copy of the List.

from better_python_lists import List

my_list = List([1, None, 2, 3, 4, None, None, 5])
compact_list = my_list.compact()
print(compact_list) # => [1, 2, 3, 4, 5]

You can also perform in-place compacting using the compacted method:

print(my_list)  # => [1, None, 2, 3, 4, None, None, 5]
my_list.compacted()
print(my_list)  # => [1, 2, 3, 4, 5]

If you want to filter out more than just None, you can pass in an optional filter list:

another_list = List([1, "None", 2, None, 3, "N/A"])
another_list.compacted(filter_list=[None, "None", "N/A"])
print(another_list)  # => [1, 2, 3]

Flatten

The methods flatten and flattened flatten an arbitrarily nested List into a single-level one.

nested_list: List = List([["a"], "b", 5, [6, 7, [8, 9, [10]]]])
flat_list = nested_list.flatten()
print(flat_list) # => ["a", "b", 5, 6, 7, 8, 9, 10]

As with compact/compacted, flatten creates a copy of the List, and flattened performs in-place replacement.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

better_python_lists-0.1.3.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

better_python_lists-0.1.3-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file better_python_lists-0.1.3.tar.gz.

File metadata

  • Download URL: better_python_lists-0.1.3.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.11.2 Darwin/22.4.0

File hashes

Hashes for better_python_lists-0.1.3.tar.gz
Algorithm Hash digest
SHA256 bf7b7a15907190d0ffef827b4f1555e0e29b252b7c3930f56e8b253fcac784c1
MD5 e8ff8b9201f0e21ebb27b8d96c49ac7f
BLAKE2b-256 bcc21c3040a2fd2dabab22178ea6f635d09c5069c5805eff0b409bb37f668379

See more details on using hashes here.

File details

Details for the file better_python_lists-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for better_python_lists-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5527dd35a233a8f2270dc74fbad75c4b20241d02461179136e4758f538b7ef66
MD5 c0ce005ba5b4765f3c7fe49c1fdc9521
BLAKE2b-256 b3941b275199eef2897be79c2f194d4ecc443143d0158195d8cc6285e8481a3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page