Python Frequently Asked Questions, Release 3.9.5 Chapter 2. Programming FAQ 22 The type constructor$_{list(seq)}$ converts any sequence or iterable into a list with the same items in the same order. For example,$_{list((1, 2, 3))}$ yields$_{[1, 2, 3]}$ and$_{list('abc')}$ yields$_{['a', 'b', 'c']}$ . If the argument is a list, it makes a copy just like$_{seq[:]}$would. For example,$_{tuple([1, 2, 3])}$ yields$_{(1, 2, 3)}$ and$_{tuple('abc')}$ yields$_{('a', 'b', 'c')}$ . If the argument is a tuple, it does not make a copy but returns the same object, so it is cheap to call$_{tuple()}$ when you aren’t sure that an object is already a tuple. The type constructor$_{tuple(seq)}$ converts any sequence (actually, any iterable) into a tuple with the same items in the same order. If you have reached the limit of what pure Python can allow, there are tools to take you further away. For example, Cython can compile a slightly modified version of Python code into a C extension, and can be used on many different platforms. Cython can take advantage of compilation (and optional type annotations) to make your code significantly faster than when interpreted. If you are confident in your C programming skills, you can also write a C extension module yourself. See also: The wiki page devoted to performance tips. str and bytes objects are immutable, therefore concatenating many strings together is inefficient as each concate- nation creates a new object. In the general case, the total runtime cost is quadratic in the total string length. To accumulate many$_{str}$ objects, the recommended idiom is to place them into a list and call$_{str.join()}$ at the end: • When the standard library provides a primitive for doing something, it is likely (although not guaranteed) to be faster than any alternative you may come up with. This is doubly true for primitives written in C, such as builtins and some extension types. For example, be sure to use either the$_{list.sort()}$ built-in method or the related$_{sorted()}$ function to do sorting (and see the sortinghowto for examples of moderately advanced usage). • Abstractions tend to create indirections and force the interpreter to work more. If the levels of indirection outweigh the amount of useful work done, your program will be slower. You should avoid excessive abstraction, especially under the form of tiny functions or methods (which are also often detrimental to readability). 2.5.1 How do I convert between tuples and lists? 2.5 Sequences (Tuples/Lists) 2.4.2 What is the most efficient way to concatenate many strings together? chunks =[] for s in my_strings: chunks.append(s) result=''.join(chunks) result=bytearray() for b in my_bytes_objects: result+=b (another reasonably efficient idiom is to use$_{io.StringIO}$) To accumulate many$_{bytes}$ objects, the recommended idiom is to extend a$_{bytearray}$ object using in-place concatenation (the$_{+=}$ operator):