The usage of append of and extend of in python list is Shared
- 2020-04-02 09:47:51
- OfStack
1. A list can contain elements of any data type, and the elements in a single list need not all be of the same type.
2.
The append() method adds a new element to the end of the list. Only one parameter is accepted.
3.
The extend() method takes only a list as an argument and adds each element of that parameter to the original list.
Example usage of append() :
> > > Mylist = [0, 1] 'ABC'
> > > mylist
[1, 2, 0, 'ABC']
> > > Mylist. Append (4)
> > > mylist
[1, 2, 0, 'ABC ', 4]
> > > Mylist. Append () 'haha'
> > > mylist
[1, 2, 0, 'ABC ', 4, 'haha']
> > >
Example of extend() usage:
> > > mylist
[1, 2, 0, 'ABC ', 4, 'haha']
> > > Mylist. The extend ([' lulu '])
> > > mylist
[1, 2, 0, 'ABC ', 4, 'haha', 'lulu']
> > > Mylist. The extend ([aaa, 'lalalala'])
Traceback (most recent call last):
The File" < stdin > ", line 1, the in < The module >
NameError: name 'aaa' is not defined
> > > Mylist. The extend ([' 123123 ', 'lalalala'])
> > > mylist
[1, 2, 0, 'ABC', 4 'haha' 'lulu', '123123', 'lalalala]
> > > Mylist. The extend ([111111222])
> > > mylist
[1, 2, 0, 'ABC', 4 'haha', 'lulu', '123123', 'lalalala, 111111, 222]
> > >
The OVER!