Tuesday, November 19, 2019

extend method in python

l1=["chicken","mutton","fish"]
l2=["biryani","fry","roast"]
l1.extend(l2)
print(l1)
print(l2)

output:
['chicken', 'mutton', 'fish', 'biryani', 'fry', 'roast']
['biryani', 'fry', 'roast']


example 2:
l1=["chicken","mutton","fish"]
l2=["biryani","fry","roast"]
l1.extend(l2)
print(l1)
print(l2)
l2.extend(l1)
print(l2)

output:

['chicken', 'mutton', 'fish', 'biryani', 'fry', 'roast']
['biryani', 'fry', 'roast']
['biryani', 'fry', 'roast', 'chicken', 'mutton', 'fish', 'biryani', 'fry', 'roast']

example3:
If we are adding a string using extend method then extire string added to given list but individual character.

l1=[10,20,30]
l2=[40,50,60]
l1.extend(l2)
print(l1)
l1.extend('anil')
print(l1)

output:
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60, 'a', 'n', 'i', 'l']

No comments:

Post a Comment

python class topic video