list.append(obj))
Add a new object at the end of the list
list.count(obj)
Count the number of times an element appears in the list
list.extend(list1)
Append multiple values in another sequence at the end of the list (extend the original list with a new list)
list.index(obj)
Find the index position of the first match of a value from the list
list.insert(index, obj)
Insert objects into the list
list.pop([index=-1])
Removes an element from the list (the last element by default) and returns the value of that element
list.remove(obj)
Remove the first match for a value in the list
list.reverse()
Elements in reverse list
list.sort(cmp=None, key=None, reverse=False)
Sort the original list
List.append() method
describe
The append() method is used to add a new object at the end of the list.
grammar
list.append(obj)
parameter
- obj – the object added to the end of the list.
Return value
There is no return value for this method, but the original list will be modified.
example
aList = [123, 'xyz', 'zara', 'abc']; aList.append( 2009 ); print "Updated List : ", aList;
Updated List : [123, 'xyz', 'zara', 'abc', 2009]
List.count() method
describe
The count() method is used to count the number of times an element appears in the list.
grammar
list.count(obj)
parameter
- obj – objects counted in the list.
Return value
Returns the number of times an element appears in the list.
example
aList = [123, 'xyz', 'zara', 'abc', 123]; print "Count for 123 : ", aList.count(123); print "Count for zara : ", aList.count('zara');
Count for 123 : 2 Count for zara : 1
List.extend() method
describe
The extend() function is used to append multiple values in another sequence at the end of the list at one time (extend the original list with a new list)
grammar
list.extend(seq)
parameter
- seq – list of elements.
Return value
The method does not return a value, but adds new list content to the existing list.
example
#!/usr/bin/python aList = [123, 'xyz', 'zara', 'abc', 123]; bList = [2009, 'manni']; aList.extend(bList) print "Extended List : ", aList ;
Extended List : [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']
List.index() method
describe
The index() function is used to find the index position of the first match of a value from the list.
grammar
list.index(x[, start[, end]])
parameter
- x -- the object to find.
- start -- optional, the starting position of the search.
- End -- optional, the end position of the search.
Return value
This method returns the index location of the lookup object, and throws an exception if the object is not found.
example
aList = [123, 'xyz', 'runoob', 'abc'] print "xyz Index location: ", aList.index( 'xyz' ) print "runoob Index location : ", aList.index( 'runoob', 1, 3 )
xyz index location: 1 runoob index location: 2
List.insert() method
describe
The insert() function inserts the specified object into the specified location in the list.
grammar
list.insert(index, obj)
parameter
- Index – the index location where the object obj needs to be inserted.
- obj – the object to insert into the list.
Return value
The method does not return a value, but inserts the object at the location specified in the list.
example
aList = [123, 'xyz', 'zara', 'abc'] aList.insert( 3, 2009) print "Final List : ", aList
Final List : [123, 'xyz', 'zara', 2009, 'abc']
List.pop() method
describe
The pop() function removes an element from the list (the last element by default) and returns the value of that element.
grammar
list.pop([index=-1])
parameter
- obj – optional parameter. To remove the index value of the list element, it cannot exceed the total length of the list. The default value is index=-1. Delete the last list value.
Return value
This method returns the element object removed from the list.
example
list1 = ['Google', 'Runoob', 'Taobao'] list_pop=list1.pop(1) print "Deleted items are :", list_pop print "The list is now : ", list1
Deleted item is: Runoob The list is now: ['google ',' Taobao ']
List.remove() method
describe
The remove() function removes the first match for a value in the list.
grammar
list.remove(obj)
parameter
- obj – the object to be removed from the list.
Return value
The method does not return a value, but removes the first match for a value in the list.
example
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.remove('Taobao') print ("The list is now : ", list1) list1.remove('Baidu') print ("The list is now : ", list1)
The list is now: ['google ',' runoob ',' Baidu '] The list is now: ['google ',' runoob ']
List.reverse() method
describe
The reverse() function is used to reverse the elements in the list.
grammar
list.reverse()
parameter
- NA.
Return value
The method does not return a value, but sorts the elements of the list in reverse.
example
#!/usr/bin/python3 list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.reverse() print ("After list reversal: ", list1)
After the list is reversed: ['baidu ',' Taobao ',' runoob ',' Google ']
List.sort() method
describe
The sort() function is used to sort the original list. If you specify parameters, the comparison function specified by the comparison function is used.
grammar
list.sort( key=None, reverse=False)
parameter
- key - the element mainly used for comparison. There is only one parameter. The specific function parameter is taken from the iteratable object. Specify an element in the iteratable object to sort.
- Reverse - collation, reverse = True descending, reverse = False ascending (default).
Return value
This method does not return a value, but sorts the objects in the list.
example
aList = ['Google', 'Runoob', 'Taobao', 'Facebook'] aList.sort() print ( "List : ", aList)
List : ['Facebook', 'Google', 'Runoob', 'Taobao']
Descending output list
# list vowels = ['e', 'a', 'u', 'o', 'i'] # Descending order vowels.sort(reverse=True) # Output results print ( 'Descending output:', vowels )
Output in descending order: ['u ',' o ',' I ',' e ',' a ']
Output the list by specifying the sorting of elements in the list
# Get the second element of the list def takeSecond(elem): return elem[1] # list random = [(2, 2), (3, 4), (4, 1), (1, 3)] # Specify the second element sort random.sort(key=takeSecond) # Output category print ('Sort list:', random)
Sort list: [(4, 1), (2, 2), (1, 3), (3, 4]]
List.clear() method
describe
The clear() function is used to clear the list, similar to del a [:].
grammar
list.clear()
parameter
- None.
Return value
The method does not return a value.
example
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.clear() print ("After the list is cleared : ", list1)
After the list is cleared: []
List.copy() method
describe
The copy() function copies the list, similar to a [:].
grammar
list.copy()
parameter
- None.
Return value
Returns the new list after replication.
example
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list2 = list1.copy() print ("list2 list: ", list2)
list2 list: ['Google', 'Runoob', 'Taobao', 'Baidu']