Python |
||
Python - Set
Set in Python is an unordered collection of distict elements. This is a data structure which is like a listbox or dropdown box you see in GUI program.
A Python set is a built-in data structure that stores an unordered collection of unique elements. Sets are mutable and can be changed after they are created. However, they do not support indexing, slicing, or other sequence-like behavior, as they are unordered. Elements within a set can be of different data types, but they must be hashable, which means that mutable types like lists and dictionaries cannot be used as elements of a set.
To create a set, you can use curly braces {} and separate the elements with commas. Alternatively, you can use the set() constructor, which can accept an iterable (such as a list or tuple) to create a new set. To create an empty set, you must use the set() constructor, as using empty curly braces {} will create an empty dictionary instead.
Some of the main features of sets include:
Python provides various built-in methods and functions to manipulate sets, such as:
Differences among Array, Set, Dictionary
Arrays, sets, and dictionaries are all collection data structures in Python, but they have some key differences in terms of their properties and use cases. Here's a summary of their main differences:
Arrays:
Sets:
Dictionaries:
Examples
Followings are the list example showing various usages and operations of Set
NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.
Examples :
|
||