Python List extend()

In this tutorial, we will see about Python List‘s extend method.Python List extend is used to add all items of iterable to end of the list.

Let’s understand about basic of python list first.

Python List is nothing but the collection of elements. You can add any data type to the list.

You can create simple list as below


Python List extend example

You can simply use extend method to add all elements of iterator to the end of the list.Python extend does not return anything but it actually updates the original list.
Let’s understand this with the help of simple example.

Output:

[1, 2, 3, 4, 5, 6]

As you can see, elements are actually added to the list rather than whole tuple as opposed to append method.
You can add all items of set,tuple or any other collection to the list.

Output:

[1, 2, 3, 4, 5, 6, ‘seven’, ‘eight’]

As you can see we have added set {‘seven’,’eight’} to the list.All the elements of set and tuples are added to the list using extend method.

That’s all about Python List extend method.


import_contacts

You may also like:

Related Posts

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • 07 January

    How to compare lists in Python

    Table of ContentsPython compare listsHow to compare lists in PythonUsing the sort() function and == operatorUsing the map() and reduce() functionsUsing the set() function and == operatorUsing the for loopUsing the set() function and - operatorUsing the collections.Counter() class and == operatorUsing the zip(), sum(), and len() functionsUsing the cmp() functionConclusion In this post, we […]

  • Python List of dictionaries
    09 December

    List of Dictionaries in Python

    Table of ContentsHow to Create a List of Dictionaries in Python?How to Access Elements of a List of Dictionaries?Search in a List of Dictionaries in PythonUpdate Values in a List of Dictionaries in PythonHow to Sort List of Dictionaries in Python?Conclusion We use both lists and dictionaries in our python programs. Lists are used to […]

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

  • Remove nan from list in Python
    16 November

    Remove nan From List In Python?

    Table of ContentsWhat is nan in python?How to remove nan from list in python?Remove nan from list using the str() functionRemove nan from list using math.isnan() functionRemove nan from list using numPy.isnan() functionRemove nan from list using pandas.isnull() functionConclusion In python, nan is a special value used to represent missing values. In this article, we […]

  • List intersection in Python
    11 November

    List intersection in python

    Table of ContentsWhat is the list intersection in Python?Ways to find the list intersection in PythonUsing the for loop to achieve list intersection in PythonUsing the set() function to find list intersection in PythonUsing the intersection() function to find list intersection in PythonUsing the filter() and lambda functions to find list intersection in PythonUsing the […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.