Python 3 - List extend() Method


Advertisements

Description

The extend() method appends the contents of seq to list.

Syntax

Following is the syntax for extend() method −

list.extend(seq)

Parameters

seq − This is the list of elements

Return Value

This method does not return any value but add the content to existing list.

Example

The following example shows the usage of extend() method.

#!/usr/bin/python3

list1 = ['physics', 'chemistry', 'maths']
list2 = list(range(5))     #creates list of numbers between 0-4
list1.extend(list2)
print ('Extended List :', list1)

Result

When we run above program, it produces the following result −

Extended List :  ['physics', 'chemistry', 'maths', 0, 1, 2, 3, 4]
python_lists.htm
Advertisements