Wednesday, May 24, 2023

Sets (in Python)

Set

  • A set is a group of items where each item is unique.
  • It is unordered (means there is no sequence and hence no index).
  • Note: Ordered means there exists a sequence in the arrangements of grouped items. Unordered means there is no sequence.
  • Ordered means that the order in which items were placed at the time of creation is maintained by the Python till no modification is done to the data itself. For ex: List and Tuple.

List Methods

Set Methods

append()

add()

extend()

update() [Note: set does not support ‘+’ op.]

remove()

remove(), discard()

pop(): removes the last element by default [Note: also accepts index to pop() an element]

pop(): we cannot say which element will be popped as a set is not ordered.

clear(): empty the list

clear(): empty the set

Intersection_update(): keeps the items in this set that are present in the other.

Now in code

thisset[0] TypeError: 'set' object is not subscriptable

Now in detail

Tags:Python,Technology,