From e154fdedef178b65e93615ae2b01097f7aef83ba Mon Sep 17 00:00:00 2001 From: Alexander Larin Date: Sat, 25 Feb 2023 13:48:28 +0300 Subject: [PATCH] Two new tricks for listtocommaseparated By using the print function and the StringIO object. --- listtocommaseparated.py2 | 13 +++++++++++++ listtocommaseparated.py3 | 11 +++++++++++ 2 files changed, 24 insertions(+) create mode 100644 listtocommaseparated.py2 create mode 100644 listtocommaseparated.py3 diff --git a/listtocommaseparated.py2 b/listtocommaseparated.py2 new file mode 100644 index 0000000..4a8c693 --- /dev/null +++ b/listtocommaseparated.py2 @@ -0,0 +1,13 @@ +from __future__ import print_function + +data = [2, 'hello', 3, 3.4] + +"""print without creating the whole string""" +print(*data, sep=',') + +"""the print function and the StringIO object""" +from cStringIO import StringIO + +out = StringIO() +print(*data, sep=',', end='', file=out) +print(out.getvalue()) diff --git a/listtocommaseparated.py3 b/listtocommaseparated.py3 new file mode 100644 index 0000000..8a4824e --- /dev/null +++ b/listtocommaseparated.py3 @@ -0,0 +1,11 @@ +data = [2, 'hello', 3, 3.4] + +"""print without creating the whole string""" +print(*data, sep=',') + +"""the print function and the StringIO object""" +from io import StringIO + +out = StringIO() +print(*data, sep=',', end='', file=out) +print(out.getvalue())