Skip to content

Commit f230bc1

Browse files
authored
Merge pull request #410 from Salehbigdeli/master
Bugfix handle edge case when we have string in list and want to flatten
2 parents cced1ec + e96545e commit f230bc1

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

fastcore/basics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ class Float(float,ShowPrint):
489489
def flatten(o):
490490
"Concatenate all collections and items as a generator"
491491
for item in o:
492+
if isinstance(item, str): yield item; continue
492493
try: yield from flatten(item)
493494
except TypeError: yield item
494495

nbs/01_basics.ipynb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2904,6 +2904,7 @@
29042904
"def flatten(o):\n",
29052905
" \"Concatenate all collections and items as a generator\"\n",
29062906
" for item in o:\n",
2907+
" if isinstance(item, str): yield item; continue\n",
29072908
" try: yield from flatten(item)\n",
29082909
" except TypeError: yield item"
29092910
]
@@ -2940,6 +2941,26 @@
29402941
"concat([(o for o in range(2)),[2,3,4], 5])"
29412942
]
29422943
},
2944+
{
2945+
"cell_type": "code",
2946+
"execution_count": null,
2947+
"metadata": {},
2948+
"outputs": [
2949+
{
2950+
"data": {
2951+
"text/plain": [
2952+
"['abc', 'xyz', 'foo', 'bar']"
2953+
]
2954+
},
2955+
"execution_count": null,
2956+
"metadata": {},
2957+
"output_type": "execute_result"
2958+
}
2959+
],
2960+
"source": [
2961+
"concat([[\"abc\", \"xyz\"], [\"foo\", \"bar\"]])"
2962+
]
2963+
},
29432964
{
29442965
"cell_type": "code",
29452966
"execution_count": null,
@@ -5306,7 +5327,7 @@
53065327
{
53075328
"data": {
53085329
"text/plain": [
5309-
"64"
5330+
"8"
53105331
]
53115332
},
53125333
"execution_count": null,

0 commit comments

Comments
 (0)