Python List
List
List is data structure in python more like array in c or java. But in python list is mutable.
List is initialized in python using list class or just [].
list1 = []
list2 = list()
Below are some methods of list object
list.append(item)
Add an Item to end of the list.list.count(item)
Returns number of time item appears in the list.list.extend(anotherList)
Extend the list by appending all the items from the given list.list.index(item, [start], [end])
Returns the first index of value. If specified optional arguments start or end, the item is searched within start and end. Raises ValueError if item not present.list.insert(index, item)
Insert an item at a given position, the element is inserted before the index.list.pop([index])
Remove an item from the list which is at index, if specified. If Not then it removes last item from list.list.remove(item)
Remove a first item from list whose value is equal to item.list.reverse()
Reverse the items of list, in place.list.sort()
sort the items of list in place.Lets understand by example:-
>>> l=[] >>> l.append(19) >>> l.append(3) >>> l.append(11) >>> l [19, 3, 11] >>> l.count(19) 1 >>> l.extend([12, 39, 5]) >>> l [19, 3, 11, 12, 39, 5] >>> l.index(39) 4 >>> l.insert(3, 19) >>> l [19, 3, 11, 19, 12, 39, 5] >>> l.remove(5) >>> l [19, 3, 11, 19, 12, 39] >>> l.pop() 39 >>> l.index(3) 1 >>> l.remove(12) >>> l [19, 3, 11, 19] >>> l.reverse() >>> l [19, 11, 3, 19] >>> l.sort() >>>l [3, 11, 19, 19]
Keep Following, For More.
Comments
Post a Comment