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

  




 

 

Chapter 16. Sets

Many algorithms need to work with simple containers of data values, irrespective of order or any key. This is a simple set of objects, which is supported by the Python set container. We'll look at Sets from a number of viewpoints: semantics, literal values, operations, comparison operators, statements, built-in functions and methods.

Set Semantics

A set is, perhaps the simplest possible container, since it contains objects in no particular order with no particular identification. Objects stand for themselves. With a sequence, objects are identified by position. With a mapping, objects are identified by some key. With a set, objects stand for themselves.

Since each object stands for itself, elements of a set cannot be duplicated. A list or tuple, for example, can have any number of duplicate objects. For example, the tuple ( 1, 1, 2, 3 ) has four elements, which includes two copies of the integer 1; if we create a set from this tuple, the set will only have three elements.

A set has large number of operations for unions, intersections, and differences. A common need is to examine a set to see if a particular object is a member of that set, or if one set is contained within another set.

A set is mutable, which means that it cannot be used as a key for a dict (see Chapter 15, Mappings and Dictionaries for more information.) In order to use a set as a dict key, we can create a frozenset, which is an immutable copy of the original set. This allows us to accumulate a set of values to create a dict key.


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