Python : How to add an element in list ? | append() vs extend()

In this article we will discuss how to add element in an existing list using different techniques.

Adding item in list using list.append()

list.append(item)

It adds the item at the end of list.

For example, we have a list of string i.e.

# List of string
wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']

Now let’s add an element at the end of this list using append() i.e.

'''
Adding item in list using list.append()
'''
wordList.append("from")

Now list will become,

['hi', 'hello', 'this', 'that', 'is', 'of', 'from']

Passing an another list as a parameter in list.append()

As list can contain different kind of elements, so if we pass an another list object as parameter in append() i.e.

'''
Passing an another list as a parameter in list.append()
''' 
wordList.append(["one", "use", "data"])

Then whole list object will be added to the end of list. So, list contents will be now,

['hi', 'hello', 'this', 'that', 'is', 'of', 'from', ['one', 'use', 'data']]

Adding all elements of one list to another using list.extend()

list.extend(list1)

It will add all elements of list1 at the end of list. Basically it will merge the two lists i.e.

wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']

'''
Adding all elements of one list to another using list.extend()
'''
wordList.extend(["one", "use", "data"])

Now list contents will be,

['hi', 'hello', 'this', 'that', 'is', 'of', 'one', 'use', 'data']

list append() vs extend()

list.append(item) , considers the parameter item as an individual object and add that object in the end of list. Even if given item is an another list, still it will be added to the end of list as individual object i.e.

wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']

wordList.append(["one", "use", "data"])

List will become,

['hi', 'hello', 'this', 'that', 'is', 'of', ['one', 'use', 'data']]

list.extend(item) , considers parameter item to be an another list and add all the individual elements of the list to the existing list i.e.

wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']

wordList.extend(["one", "use", "data"])

List will become,

['hi', 'hello', 'this', 'that', 'is', 'of', 'one', 'use', 'data']

Complete example is as follows,

"""
    Python : How to add element in list | append() vs extend()
"""

def main():

    # List of string
    wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']
    
    # print the List
    print(wordList)
    
    '''
    Adding item in list using list.append()
    '''
    wordList.append("from")
    
    # print the List
    print(wordList)
   
    '''
    Passing an another list as a parameter in list.append()
    ''' 
    wordList.append(["one", "use", "data"])
    
    # print the List
    print(wordList)
    

    wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']
    
    '''
    Adding all elements of one list to another using list.extend()
    '''
    wordList.extend(["one", "use", "data"])
    
    # print the List
    print(wordList)

if __name__ == "__main__":
    main()
    

Output

['hi', 'hello', 'this', 'that', 'is', 'of']
['hi', 'hello', 'this', 'that', 'is', 'of', 'from']
['hi', 'hello', 'this', 'that', 'is', 'of', 'from', ['one', 'use', 'data']]
['hi', 'hello', 'this', 'that', 'is', 'of', 'one', 'use', 'data']

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top