TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Append Vs. Extend in Python List
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python BasicPython Tutorials

Append Vs. Extend in Python List

Last updated: Oct 07, 2023 1:49 pm
By Meenakshi Agarwal
Share
9 Min Read
Python difference between append and extend
Python difference between append and extend
SHARE

From this tutorial, you can explore the difference between append and extend methods of Python List. Both these methods are used to manipulate the lists in their specific way.

Contents
Python Append()Python Extend()Summary – Append Vs. Extend

The append method adds a single or a group of items (sequence) as one element at the tail of a list. On the other hand, the extend method appends the input elements to the end as part of the original list.

After reading the above description about append() and extend(), it may seem a bit confusing to you. So, we’ll explain each of these methods with examples and show the difference between them.

Python List

Difference between Append and Extend – Python List

It is quite common for programmers to create and use lists in their programs. However, there might also be situations when they need to extend the existing lists.

Python provides two distinct methods (append() and extend()) to expand lists at runtime. Each of these has some unique characteristics and differences. We are going to highlight them using flowcharts and coding examples.

So, let’s first begin describing the append method.

Python Append()

This method modifies the existing list by appending a single element to the tail. It means that the append() method adds a single element to the list, be it one item or a sequence such as a list, tuple, set, etc.

Below is the Python syntax for the append() method:

# Python append() - List method
List.append(number|string|list|tuple|set|dict)

Here, you can see that we can pass one argument of any data type out of a number of supported ones. It could be a singular item (like a number or string) or a sequence (list, tuple, etc.). We have brought a simple diagram representing the Append flow:

Python difference between append and extend

The picture shows two simple use cases: The first appending one item and the second is adding a list with two elements.

It shows that we have an initial list of four elements, say integers. We are then pushing a new number ‘b1’ to the list using the append method.

Since we added a single item, the list now has five elements with ‘b1’ appended at the end. In the second case, the argument is a list of two integers, c1 and c2. Hence, the append() method will add it as one item in the existing list.

You can also see that the output list of the second use case is showing a List within the List.

Below is the sample code to demonstrate these two use cases.

"""
 Desc:
  Python program to demonstrate list append() function
"""

# Use case#1 - Add a single number to the list
element = 50

# Main list to append new element
in_list = [10, 20, 30, 40]

# Call list append()
in_list.append(element)

# print the output
print("*** Use case#1 ***********")
print("The list: ", in_list)
print("The list size: ", len(in_list))

# Use case#2 - Add multiple numbers to the list
elements = [50, 60]

# Main list to append new elements
in_list = [10, 20, 30, 40]

# Call list append()
in_list.append(elements)

# print the output
print("\n*** Use case#2 ***********")
print("The list: ", in_list)
print("The list size: ", len(in_list))

You can copy/paste the above code and run it. You will see the below output.

*** Use case#1 ***********
The list:  [10, 20, 30, 40, 50]
The list size:  5

*** Use case#2 ***********
The list:  [10, 20, 30, 40, [50, 60]]
The list size:  5

It is evident from the results that the list grew by one in both use cases. It also proves our point that the append method adds only one element.

Next, let’s unwrap the Python extend() method to know the difference between append and extend functions.

List Append

Python Extend()

The extend() method lives up to its name and appends elements at the end of a list.

Unlike append, it only takes an iterable type argument and increases the list length by the number of elements added.

The syntax for the extend() is:

# Python extend() - List method
List.append(itereable)

An iterable is a kind of sequence that implements the __iter__() method. And you can iterate through it like for lists, tuples, strings, dictionaries, etc.

Moreover, we have brought a simple diagram representing the Extend function flow:

Python difference between append and extend

This picture also shows two simple use cases. The first is extending the list by one and another with two elements.

It presents the following facts before us:

  • We have an input list of four elements (all integers).
  • In the first case, the item to add is a new number b1 using the list extend method.
  • When we extend the list by one item, it gets five elements with b1 at the end.
  • In the second use case, the argument is a list of two integers, c1 and c2. Hence, the Extend method adds all its items as is in the existing list.
  • You can also see that the resultant is a regular list with six elements.

Below is the sample code to demonstrate the above two use cases.

"""
 Desc:
  Python program to demonstrate list extend() function
"""

# Use case#1 - Extend list by a single element
element = [50] # Note: We can't directly pass a number, it should be an iterable.

# Main list to extend by one element
in_list = [10, 20, 30, 40]

# Call list extend()
in_list.extend(element)

# print the output
print("*** Use case#1 ***********")
print("The list: ", in_list)
print("The list size: ", len(in_list))

# Use case#2 - Extend list by multiple elements
elements = [50, 60]

# Main list to extend by new elements
in_list = [10, 20, 30, 40]

# Call list extend()
in_list.extend(elements)

# print the output
print("\n*** Use case#2 ***********")
print("The list: ", in_list)
print("The list size: ", len(in_list))

You can copy/paste the above code and run it. You will see the below output.

*** Use case#1 ***********
The list:  [10, 20, 30, 40, 50]
The list size:  5

*** Use case#2 ***********
The list:  [10, 20, 30, 40, 50, 60]
The list size:  6

It is evident from the results that the list grew by no. of new elements in both use cases. It also justifies our point that the Extend method adds all elements to the list.

List Extend

Summary – Append Vs. Extend

We’ve now seen that both methods grow the list but a little differently. Here, we are summing up the key differences between the append and extend.

  • Both extend() and append() are built-in list extension methods.
  • Append accepts all data types and adds only one element to the list.
  • Extend accepts only iterable types and appends all elements to the list.

We should also consider the difference in their efficiency. The time complexity is the easiest way to assess each method. We are referring to what python.org mentions on their wiki page.

+-----------+-----------------+
| Method    | Time Complexity |
+-----------+-----------------+
| append()  |  O(1)           |
| extend()  |  O(k)           |
+-----------+-----------------+

Here, “k” is the number of elements in the iterable parameter of the Extend method.

We hope that after wrapping up this tutorial, you have enough knowledge to define Python Append vs. Extend. However, to learn more about Python, read our step-by-step Python tutorial.

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

Selenium Python Extent Report Guide

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Python map() function with examples Python Map() Explained with Examples
Next Article Python filter() function with examples Python Filter()

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x