Python list append() method is used to add an element to the end of the list.
Syntax
list.append(item)
Parameters
item(required): It is the item(numbers, strings, another list, a dictionary) you want to add to the end of the list
Return value
The method doesn’t return any value (returns None).
Visual Representation
Example 1: How to Use list append() Method
numbers = [1, 2, 3]
numbers.append(4)
numbers
Output
Example 2: Adding list to list
GoT = ['Daenerys', 'Jon', 'Tyrion']
Friends = ['Rachel', 'Monica', 'Phoebe']
GoT.append(Friends)
print(GoT)
Output
['Daenerys', 'Jon', 'Tyrion', ['Rachel', 'Monica', 'Phoebe']]
Time Complexity for Append in Python
The list append() method has constant time complexity, i.e., O(1), because lists are randomly accessed so that the last element can be reached in O(1) time. That’s why the time taken to add the new element at the end of the list is O(1).
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.