Adding elements to a list is a common operation in Python programming. In this blog post, we’ll explore different code examples that demonstrate how to add an element to a list efficiently.
Example 1:
fruits = ['apple', 'banana', 'orange', 'kiwi'] fruits.append('mango') print(fruits)
Output: [‘apple’, ‘banana’, ‘orange’, ‘kiwi’, ‘mango’]
Example 2:
numbers = [1, 2, 3, 4, 5] numbers.append(6) print(numbers)
Output: [1, 2, 3, 4, 5, 6]