Skip to main content
Help the Python Software Foundation raise $60,000 USD by December 31st!  Building the PSF Q4 Fundraiser

iterable as a chain

Project description

ChIter - iterable as a chain


  • Python 3.7+


pip install chiter


Coming soon


  • Chains do not require saving the intermediate state in temporary variables
  • Look more readable


It is necessary to get the sum of all numbers from the following sequence:"23,45,67\n45,56,55\n\n45,a,5\n-45,56,0"

first way

from itertools import chain

data = "23,45,67\n45,56,55\n\n45,a,5\n-45,56,0"

chunks = (chunk.split(',') for chunk in data.split())
flat_data = chain.from_iterable(chunks)
items = (int(item) for item in flat_data if not item.isalpha())
result = sum(items)

assert result == 352

second way

from itertools import chain

data = "23,45,67\n45,56,55\n\n45,a,5\n-45,56,0"

result = sum((
    for item in chain.from_iterable(map(lambda c: c.split(','), data.split()))
    if not item.isalpha()
assert result == 352

chiter way

from chiter import ChIter as I

data = "23,45,67\n45,56,55\n\n45,a,5\n-45,56,0"

result = (I(data.split())
          .map(lambda x: x.split(','))

assert result == 352

Related Libraries

Project details

Download files

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

Files for chiter, version 1.0.0a2
Filename, size File type Python version Upload date Hashes
Filename, size chiter-1.0.0a2-py3-none-any.whl (5.1 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size chiter-1.0.0a2.tar.gz (4.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page