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 Statements

The Assignment Statement. The variation on the assignment statement called multiple-assignment statement also works with lists. We looked at this in the section called “Multiple Assignment Statement”. Multiple variables are set by decomposing the items in the list.

>>> 
x,y=[1,"hi"]

>>> 
x

1
>>> 
y

"hi"
            

This will only work of the list has a fixed and known number of elements. This is more typical when working with tuples, which are immutable, rather than lists, which can vary in length.

The for Statement. The for statement also works directly with sequences like list. The range function that we have used creates a list. We can also create lists other ways. We'll touch on various list construction techniques at several points in the text.

s= 0
for i in [2,3,5,7,11,13,17,19]:
    s += i
print "total",s

The del Statement. The del statement removes items from a list. For example

>>> 
i = range(10)

>>> 
del i[0],i[2],i[4],i[6]

>>> 
i

[1, 2, 4, 5, 7, 8]
            

This example reveals how the del statement works.

The i variable starts as the list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ].

Remove i[0] and the variable is [1, 2, 3, 4, 5, 6, 7, 8, 9].

Remove i[2] (the value 3) from this new list, and get [1, 2, 4, 5, 6, 7, 8, 9].

Remove i[4] (the value 6) from this new list and get [1, 2, 4, 5, 7, 8, 9].

Finally, remove i[6] and get [1, 2, 4, 5, 7, 8].


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