To add new values to a list, use the append() and insert() methods. Enter the following into the interactive shell to call the append() method on a list value stored in the variable cars.
>>> cars = ['Benz','Audi','Lexus','Jeep']
>>> cars.append('BMW')
>>> cars
['Benz', 'Audi', 'Lexus', 'Jeep', 'BMW']
>>>
The previous append() method call adds the argument to the end ofthe list. The insert() method can insert a value at any index in the list.The first argument to insert() is the index for the new value, and the second argument is the new value to be inserted. Enter the following into the interactive shell.
>>> Languages = ['Python','Javascript','Php','Rust']
>>> Languages.insert(2,'Golang')
>>> Languages
['Python', 'Javascript', 'Golang', 'Php', 'Rust']
>>>
InClusion.
Methods belong to a single data type .The append() and insert() methods are list methods and can be called on list values , not on other values such as strings or integers.
Top comments (0)