append ()

The syntax for the append () method is given as follows:

list.append () 

The method adds a value at the end of the list. Let's see the following example:

>>> Avengers = []

>>> Avengers.append("Captain-America")

>>> Avengers.append("Iron-man")

>>> Avengers

['Captain-America', 'Iron-man']

>>>

You can see that we have added two members to the list.

Consider a situation where you want to add a list to an existing list. For example, we have two lists of our heroes:

Avengers1 = ['hulk', 'iron-man', 'Captain-America', 'Thor']

Avengers2 = ["Vision","sam"]

We want to add the Avengers2 list to the Avengers1 list. If you are thinking about the + operator, you might be right to some extent but not completely because the + operator just shows the addition but doesn't change the original lists.

In order to add one list to another, we will use the extend () method. See the syntax explained in the subsequent subsection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.197.95