Add List Items
Add List Items
There are three methods to add items to list: append(), insert() and extend()
append():
This method appends items to the end of the existing list.
Example:
Output:
What if you want to insert an item in the middle of the list? At a specific index?
insert():
This method inserts an item at the given index. User has to specify index and the item to be inserted within the insert() method.
Example:
Output:
What if you want to append an entire list or any other collection (set, tuple, dictionary) to the existing list?
extend():
This method adds an entire list or any other collection datatype (set, tuple, dictionary) to the existing list.
Example 1:
Output:
Example 2:
Output:
Example 3:
Output:
Example 4:
Output:
concatenate two lists:
you can simply concatenate two list to join two lists.
Example:
Output: