diff --git a/fastcore/basics.py b/fastcore/basics.py index 82a2610e..96b6e012 100644 --- a/fastcore/basics.py +++ b/fastcore/basics.py @@ -489,6 +489,7 @@ class Float(float,ShowPrint): def flatten(o): "Concatenate all collections and items as a generator" for item in o: + if isinstance(item, str): yield item; continue try: yield from flatten(item) except TypeError: yield item diff --git a/nbs/01_basics.ipynb b/nbs/01_basics.ipynb index d0f6b8e9..4501eb98 100644 --- a/nbs/01_basics.ipynb +++ b/nbs/01_basics.ipynb @@ -2904,6 +2904,7 @@ "def flatten(o):\n", " \"Concatenate all collections and items as a generator\"\n", " for item in o:\n", + " if isinstance(item, str): yield item; continue\n", " try: yield from flatten(item)\n", " except TypeError: yield item" ] @@ -2940,6 +2941,26 @@ "concat([(o for o in range(2)),[2,3,4], 5])" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['abc', 'xyz', 'foo', 'bar']" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "concat([[\"abc\", \"xyz\"], [\"foo\", \"bar\"]])" + ] + }, { "cell_type": "code", "execution_count": null, @@ -5306,7 +5327,7 @@ { "data": { "text/plain": [ - "64" + "8" ] }, "execution_count": null,