Skip to content

"Mutable and Immutable Types" example #298

@ghost

Description

The best example in the Mutable and Immutable Types section of the Structuring Your Project page is:

# create a concatenated string from 0 to 19 (e.g. "012..1819")
nums = [str(n) for n in range(20)]
print "".join(nums)

It is considered the best example for being both more efficient and succinct.

Would this be a better example to use?

''.join(map(str, range(20)))

It's more succinct and is faster:

$ python -m timeit "nums = [str(n) for n in range(20)]; ''.join(nums)"
100000 loops, best of 3: 6.84 usec per loop

$ python -m timeit "''.join(map(str, range(20)))"
100000 loops, best of 3: 4.93 usec per loop

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions