Append and extend methods in Python

Differences between two Python methods with code examples
22 August 2017   2140

Our team made a research and found what are the differences between these to methods.

  • append adds its argument as a single element to the end of a list. The length of the list itself will increase by one.
  • extend iterates over its argument adding each element to the list, extending the list. The length of the list will increase by however many elements were in the iterable argument.

append

The list.append method appends an object to the end of the list.

my_list.append(object) 

Whatever the object is, whether a number, a string, another list, or something else, it gets added onto the end of my_list as a single entry on the list.

>>> my_list
['foo', 'bar']
>>> my_list.append('baz')
>>> my_list
['foo', 'bar', 'baz']

So keep in mind that a list is an object. If you append another list onto a list, the first list will be a single object at the end of the list (which may not be what you want):

>>> another_list = [1, 2, 3]
>>> my_list.append(another_list)
>>> my_list
['foo', 'bar', 'baz', [1, 2, 3]]
                     #^^^^^^^^^--- single item on end of list.

extend

The list.extend method extends a list by appending elements from an iterable:

my_list.extend(iterable)

So with extend, each element of the iterable gets appended onto the list. For example:

>>> my_list
['foo', 'bar']
>>> another_list = [1, 2, 3]
>>> my_list.extend(another_list)
>>> my_list
['foo', 'bar', 1, 2, 3]

Keep in mind that a string is an iterable, so if you extend a list with a string, you'll append each character as you iterate over the string (which may not be what you want):

>>> my_list.extend('baz')
>>> my_list
['foo', 'bar', 1, 2, 3, 'b', 'a', 'z']

 

PyOxidizer to be Available

Project, allowing  package a Python project in the form of a self-contained executable file is written in Rust and has MPL license
25 June 2019   164

The first release of the PyOxidizer utility is available, which allows you to package a Python project in the form of a self-contained executable file, including the Python interpreter and all the necessary libraries and resources. Such files can run in environments without the Python toolkit installed or regardless of the availability of the required version of Python. PyOxidizer can also generate statically related executables that are not associated with system libraries. The project code is written in the Rust language and is distributed under the MPL (Mozilla Public License) 2.0 license.

The project is based on the same-name module for the Rust language, which allows you to embed a Python interpreter into Rust programs to run Python scripts in them. PyOxidizer has now gone beyond the add-on for Rust and is positioned as a tool available to a wider audience for building and distributing self-contained Python packages. For those who do not need to distribute applications in the form of an executable file, PyOxidizer provides opportunities for generating libraries suitable for linking with any applications for embedding the Python interpreter and the necessary set of extensions in them.

For end users, delivering a project in the form of a single executable file greatly simplifies installation and eliminates the work of selecting dependencies, which is important, for example, for complex Python projects, such as video editors. For application developers, PyOxidizer allows you to save time on organizing the delivery of an application without having to use different tools for building packages for different operating systems.

The use of the proposed builds also has a positive effect on performance - files generated in PyOxidizer run faster than using system Python by eliminating imports and defining basic modules. In PyOxidizer, modules are imported from memory - all built-in modules are immediately loaded into memory and then used without accessing the disk). In tests, application startup time using PyOxidizer is approximately halved.

From already existing similar projects it is possible to note: PyInstaller (unpacks the file into a temporary directory and imports modules from it), py2exe (tied to the Windows platform and requires distribution of several files), py2app (tied to macOS), cx-freeze (requires separate packaging of dependencies ), Shiv and PEX (form a package in zip format and requires Python in the system), Nuitka (compiles the code, not the interpreter embeds), pynsist (tied to Windows), PyRun (proprietary development without explanation of the principles of operation).

At the current stage of development, PyOxidizer has already implemented the main functionality for generating executable files for Windows, macOS and Linux. Of the currently unavailable features, there is a lack of a typical build environment, the impossibility of generating packages in the MSI, DMG and deb / rpm formats, problems with packaging projects that include complex extensions in the C language, and the lack of commands to maintain delivery ("pyoxidizer add" and "pyoxidizer upgrade"), limited support for Terminfo and Readline, lack of support for releases other than Python 3.7, lack of support for resource compression, inability to cross-compile.