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 Literal Values

A list is created by surrounding them with []'s and separating the values with commas (,). A list can be created, expanded and reduced. An empty list is simply []. As with tuples, an extra comma at the end of the list is graciously ignored.

Examples:

myList = [ 2, 3, 4, 9, 10, 11, 12 ]
history = [ ]

The elements of a list do not have to be the same type. A list can be a mixture of any Python data types, including lists, tuples, strings and numeric types.

A list permits a sophisticated kind of display called a comprehension. We'll revisit this in some depth in the section called “List Comprehensions”. As a teaser, consider the following:

>>> 
print [ 2*i+1 for i in range(6) ]

[1, 3, 5, 7, 9, 11]
            

This statement creates a list using a list comprehension. A comprehension starts with a candidate list (range( 6 ), in this example) and derives the list values from the candidate list (using 2*i+1 in this example). A great deal of power is available in comprehensions, but we'll save the details for a later section.


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