How to append element in the list
Python provides built-in methods to append or add elements to the list. We can also append a list into another list. These methods are given below.
Let's understand these methods by the following example.
This function is used to add the element at the end of the list. The example is given below.
Current names List is: ['Joseph', 'Peter', 'Cook', 'Tim'] Please enter a name: Devansh Updated name List is: ['Joseph', 'Peter', 'Cook', 'Tim', 'Devansh']
2. insert(index, elmt)
The insert() function adds the elements at the given an index position. It is beneficial when we want to insert element at a specific position. The example is given below.
Current Numbers List: [10, 20, 30, 40, 50] The new list is: [10, 20, 30, 77, 40, 50] enter a number to add to list: 45 enter the index to add the number: 1 Updated Numbers List: [10, 45, 20, 30, 77, 40, 50]
The extends() function is used to add the iterable elements to the list. It accepts iterable object as an argument. Below is the example of adding iterable element.
[10, 20, 30, '52.10', '43.12'] [10, 20, 30, '52.10', '43.12', 40, 30] [10, 20, 30, '52.10', '43.12', 40, 30, 'A', 'p', 'p', 'l', 'e']
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week