📜  Python list append()(1)

📅  最后修改于: 2023-12-03 15:04:05.887000             🧑  作者: Mango

Python list append()

The append() method in Python is used to add an element to the end of a list. It modifies the original list and returns None.

Syntax

The syntax for append() is:

list.append(element)

Where:

  • list is the list to which the element will be appended.
  • element is the object that is added to the end of the list.
Example
fruits = ['apple', 'banana', 'mango']
fruits.append('orange')
print(fruits)

Output:

['apple', 'banana', 'mango', 'orange']
Key Points
  1. The append() method adds an element to the end of a list.
  2. It modifies the original list and returns None.
  3. The element can be of any data type: string, number, list, tuple, etc.
  4. It can also append another list as a single element.
  5. Multiple elements can be appended by calling append() method multiple times.
  6. It is a convenient way to dynamically expand a list.
Important Considerations
  1. Keep in mind that append() does not create a new list, it modifies the original list.
  2. The original list object is mutable and the changes are made in-place.
  3. If you want to append multiple elements at once, consider using the extend() method instead.
  4. Be aware that if you try to append a list to itself, it will result in an infinite loop.

For more details, refer to the official Python documentation here.