11 of 151 menu

The extend method

The extend method adds elements from the specified object, which can be another list, tuple, or set, to the list. In the method parameter, we specify the object we need to add.

Syntax

list.extend(object to add)

Example

Let's add another list to our list:

lst1 = ['a', 'c'] lst2 = ['1', '2'] lst1.extend(lst2) print(lst1)

Result of code execution:

['a', 'b', '1', '2']

Example

Now let's add a tuple to our list:

lst = ['a', 'b'] tpl = ('1', '2') lst.extend(tpl) print(lst)

Result of code execution:

['a', 'b', '1', '2']

Example

Let's add a lot to our list:

lst = ['a', 'b'] st = {'e', 'f'} lst.extend(st) print(lst)

Result of code execution:

['a', 'b', 'e', 'f']

See also

  • method append,
    which adds an element to the end of a list
  • method copy,
    which copies the list
  • method reverse,
    which reverses the list
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline