We can add items using append and insert class. In this section, I will show you how we can add and remove items from a list.
numbers = ['one', 'two', 'three', 'four', 'fine', 'six']
numbers.append("seven")
print(numbers)
Output:
numbers = ['one', 'two', 'three', 'four', 'fine', 'six']
numbers.append("seven")
print(numbers)
numbers = ['one', 'two', 'three', 'four', 'fine', 'six']
numbers.insert(1, "item")
print(numbers)
Output:
['one', 'item', 'two', 'three', 'four', 'fine', 'six']
one = ['one', 'two', 'three']
two = ['four', 'fine', 'six']
one.extend(two)
print(one)
Output:
['one', 'two', 'three', 'four', 'fine', 'six']
one = ['one', 'two', 'three']
two = ['four', 'fine', 'six']
print(one + two)
Output:
['one', 'two', 'three', 'four', 'fine', 'six']
numbers = ['one', 'two', 'three', 'four', 'fine', 'six']
numbers.remove("three")
Output:
['one', 'two', 'four', 'fine', 'six']
numbers = ['one', 'two', 'three', 'four', 'fine', 'six']
numbers.pop()
Output:
['one', 'two', 'three', 'four', 'fine']
numbers = ['one', 'two', 'three', 'four', 'fine', 'six']
numbers.pop(1)
Output:
['one', 'three', 'four', 'fine', 'six']
numbers = ['one', 'two', 'three', 'four', 'fine', 'six']
del numbers[0]
Output:
['two', 'three', 'four', 'fine', 'six']
numbers = ['one', 'two', 'three', 'four', 'fine', 'six']
numbers.clear()
print(numbers)
Output:
[]