DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 27 of 77
←PreviousPrevNextNext→

Add List Items

Addition Methods

Lists provide several ways to add elements. The append() method adds a single item at the end. The extend() method adds all items from another iterable. The insert() method adds an item at a specified position. You can also use concatenation with + to create a new list or += to extend the existing list.

Example
fruits = ['apple']

# Append single item
fruits.append('banana')

# Extend with multiple items
fruits.extend(['cherry', 'date'])

# Insert at position
fruits.insert(1, 'avocado')

# Concatenation
new_fruits = fruits + ['elderberry', 'fig']
print(fruits)
print(new_fruits)
Output
['apple', 'avocado', 'banana', 'cherry', 'date']
['apple', 'avocado', 'banana', 'cherry', 'date', 'elderberry', 'fig']

Performance Considerations

Not all addition methods perform equally. append() is generally very efficient (amortized O(1)), while inserting at the beginning of a list requires shifting all elements and is O(n). Concatenation with + creates a new list, which can be less efficient if repeated often.

Example
from timeit import timeit

# Compare common operations
print("append:", timeit('lst.append(1)', 'lst = []'))
print("insert(0):", timeit('lst.insert(0, 1)', 'lst = []'))
print("concatenation:", timeit('lst + [1]', 'lst = []'))
ℹ️ Note: Use append() or extend() for efficient growth. Avoid repeatedly using + in a loop.

Bulk Addition Patterns

When adding multiple items at once, you can use extend(), +=, or unpacking syntax. These approaches are flexible and efficient for combining lists or adding generated values.

Example
# Adding from another list
main = [1, 2, 3]
main += [4, 5, 6]  # In-place extension

# Adding from generator
def squares(n):
    for i in range(n):
        yield i**2

numbers = [1, 2, 3]
numbers.extend(squares(3))  # [1, 2, 3, 0, 1, 4]

# Adding with unpacking
parts = ['head', 'shoulders']
full = [*parts, 'knees', 'toes']
Output
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 0, 1, 4]
['head', 'shoulders', 'knees', 'toes']
Test your knowledge: Add List Items
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 27 of 77
←PreviousPrevNextNext→