Python List and Method
Create empty list :
>>> list = []
1)
Add an item to the end of the list
>>> list.append(1)
2)
Extend the list by appending all the items in the given list
>>>list1[1,2,3,4,5]
>>>list.extend(list1)
3)
Insert an item at a given position. The first argument is the index of the element before which to insert.
>>>list.insert(0,3)
4)
Remove the first item from the list whose value is 4. It is an error if there is no such item.
>>>list.remove(4)
5)
Remove the item at the given position in the list, and return it. If no index is specified,
>>>list.pop(1)
6)
Return the index in the list of the first item whose value is x.
>>> list.index(3)
7)
Return the number of times x appears in the list.
>>> list.count(3)
8)
Sort the items of the list.
>>>list.sort()
9)
Reverse the elements of the list
>>>list.reverse()
>>> list = []
1)
Add an item to the end of the list
>>> list.append(1)
2)
Extend the list by appending all the items in the given list
>>>list1[1,2,3,4,5]
>>>list.extend(list1)
3)
Insert an item at a given position. The first argument is the index of the element before which to insert.
>>>list.insert(0,3)
4)
Remove the first item from the list whose value is 4. It is an error if there is no such item.
>>>list.remove(4)
5)
Remove the item at the given position in the list, and return it. If no index is specified,
list.pop()
removes and returns the last item in the list.>>>list.pop(1)
6)
Return the index in the list of the first item whose value is x.
>>> list.index(3)
7)
Return the number of times x appears in the list.
>>> list.count(3)
8)
Sort the items of the list.
>>>list.sort()
9)
Reverse the elements of the list
>>>list.reverse()
Comments
Post a Comment