How to Use List extend() Method in PythonPython Basics


The extend() method extends the list by adding all elements of a list to the end of it.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

list1 = ['MIT', 'CIT', 'Yale','UCI']
print("the list1 is: ", list1)
list2= [ 'Yale','UCI']
print("the list2 is: ", list2)

list1.extend(list2)
print("the new list1 is  :", list1)

Output:

the list1 is:  ['MIT', 'CIT', 'Yale', 'UCI']
the list2 is:  ['Yale', 'UCI']
the new list1 is  : ['MIT', 'CIT', 'Yale', 'UCI', 'Yale', 'UCI']

Syntax

list.extend(seq)

Parameters

Name Description
seq A list of elements, which can be lists, tuples, collections, dictionaries.

Return Value

It doesn’t return any value.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us