Difference between Append()
and Extend()
when acting on a list
Consider myList
Append()
myList= ['a','3','d','t'] myList.append(['c','d']) print(myList)
=============OUTPUT ===============
[ 'a' , '3' , 'd' , 't' , [ 'c' , 'd' ] ]
====================================
Extend()
myList= ['a','3','d','t'] myList.extend(['c','d']) print(myList)
=============OUTPUT ===============
[ 'a' , '3' , 'd' , 't' , 'c' , 'd' ]
====================================
Ooooooooooooooh. I’ve always had trouble with this. When you append values to Python list, you’ll get a list within a list?
LikeLike
Yes, thats right. Or you can add an element using append like `myList.append(‘c’)` which will return `[ ‘a’ , ‘3’ , ‘d’ , ‘t’ , ‘c’ ]`
LikeLiked by 1 person