Add element to the beginning of a list Python
Ngày đăng:
19/01/2022
Trả lời:
0
Lượt xem:
81
Python - Add List Items
Previous
Next
Append ItemsTo add an item to the end of the list, use the append() method: ExampleUsing the append() method to append an item:
thislist = ["apple", "banana", "cherry"]
Try it Yourself »
Insert ItemsTo insert a list item at a specified index, use the insert() method. The insert() method inserts an item at the specified index: ExampleInsert an item as the second position:
thislist = ["apple", "banana", "cherry"]
Try it Yourself »
Note: As a result of the examples above, the lists will now contain 4 items. Extend ListTo append elements from another list to the current list, use the extend() method. ExampleAdd the elements of tropical to thislist:
thislist = ["apple", "banana", "cherry"]
Try it Yourself »
The elements will be added to the end of the list. Add Any IterableThe extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.). ExampleAdd elements of a tuple to a list:
thislist = ["apple", "banana", "cherry"]
Try it Yourself »
Previous
Next
|