Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

List Methods

A list object has a number of member methods. These can be grouped arbitrarily into transformations, which change the list, and information, which returns a fact about a list. In all of the following method functions, we'll assume a list object named l .

The following list transformation functions update a list object. In the case of the pop method, it both returns information as well as updates the list.

l. append ( object )

Update list l by appending object to end of the list.

l. extend ( list )

Extend list l by appending list elements. Note the difference from append( object ), which treats the argument as a single list object.

l. insert ( index , object )

Update list l by inserting object before position index . If index is greater than len( list ), the object is simply appended. If index is less than zero, the object is prepended.

l. pop ( [index] ) → item

Remove and return item at index (default last, -1) in list l . An exception is raised if the list is already empty.

l. remove ( value ) → item

Remove first occurrence of value from list l . An exception is raised if the value is not in the list.

l. reverse

Reverse the items of the list l . This is done "in place", it does not create a new list.

l. sort ( [cmpfunc] )

Sort the items of the list l . This is done "in place", it does not create a new list. If a comparison function, compare is given, it must behave like the built-in cmp: cmpfunc( x , y ) → -1, 0, 1.

The following accessor methods provide information about a list.

l. count ( value ) → integer

Return number of occurrences of value in list l .

l. index ( value ) → integer

Return index of first occurrence of value in list l .

Stacks and Queues. The append and pop functions can be used to create a standard push-down stack, or last-in-first-out (LIFO) list. The append function places an item at the end of the list (or top of the stack), where the pop function can remove it and return it.

>>> 
stack= []

>>> 
stack.append(1)

>>> 
stack.append("word")

>>> 
stack.append( ("a","2-tuple") )

>>> 
stack.pop()

('a', '2-tuple')
>>> 
stack.pop()

'word'
>>> 
stack.pop()

1
            

The append and pop( 0 ) functions can be used to create a standard queue, or first-in-first-out (FIFO) list. The append function places an item at the end of the queue. A call to pop( 0 ) removes the first item from the queue it and returns it.

>>> 
queue=[]

>>> 
queue.append(1)

>>> 
queue.append("word")

>>> 
queue.append( ("a","2-tuple") )

>>> 
queue.pop(0)

1
>>> 
queue.pop(0)

'word'
>>> 
queue.pop(0)

('a', '2-tuple')
            

 
 
  Published under the terms of the Open Publication License Design by Interspire