The append() method adds a single item to the existing list. It doesn't return a new list; rather it modifies the original list.
The syntax of append() method is:
list.append(item)
The append() method takes a single item and adds it to the end of the list.
The item can be numbers, strings, another list, dictionary etc.
As mentioned, the append() method only modifies the original list. It doesn't return any value.
# animal list animal = ['cat', 'dog', 'rabbit'] # an element is added animal.append('guinea pig') #Updated Animal List print('Updated animal list: ', animal)
When you run the program, the output will be:
Updated animal list: ['cat', 'dog', 'rabbit', 'guinea pig']
# animal list animal = ['cat', 'dog', 'rabbit'] # another list of wild animals wild_animal = ['tiger', 'fox'] # adding wild_animal list to animal list animal.append(wild_animal) #Updated List print('Updated animal list: ', animal)
When you run the program, the output will be:
Updated animal list: ['cat', 'dog', 'rabbit', ['tiger', 'fox']]
It's important to notice that, a single item (wild_animal list) is added to the animal list in the above program.
If you need to add items of a list to the another list (rather than the list itself), extend() method is used.
It takes a lot of effort and cost to maintain Programiz. We would be grateful if you support us by either:
Disabling AdBlock on Programiz. We do not use intrusive ads.
or
Donate on Paypal