Advanced Commands

Advanced Python Commands

Set Commands

Sets are also used to store multiple elements in a single object but they do not allow duplicate elements. Sets are unordered and unindexed. That means if you print all elements of a set they will get printed in random order. Once set is created you can not modify the elements but you can add new elements or can also remove existing elements. Now, let’s discuss some important set commands that python provides as built-in methods.

add()

add() command is used to add a new element in the set. 

Syntax: setname.add(element)

  • setname is the name of that set variable in which you want to add a new element.
  • element refers to the new item that you want to add. 

clear()

clear() removes all the elements of a set. It does not take any parameters.

Syntax: setname.clear()

discard()

discard() is used to remove the specified element from the set. If the specified element is not found in the set it will not give an error. 

Syntax: setname.discard(element)

  • setname is the name of that set variable from which you want to remove an element.
  • element refers to the element that you want to remove.

remove()

remove command is also used to remove a specified element from the set but it is different from discard because remove will give an error if the specified element is not found in the set.

Syntax: setname.remove(element)

  • setname is the name of that set variable from which you want to remove an element.
  • element refers to the element that you want to remove.

difference()

difference method is used to get a set that contains the difference of two sets. The difference of sets means it will have only those elements that are present in only one set and not in another set. Suppose we have two sets A and B. Set A has {1,2,3} and set B has {2, 4, 6}. Then the difference of A and B will be {1,3}. 

Syntax: setA.difference(setB)

  • Elements of setB will be removed from setA if present.

difference_update()

difference_update method is used to get a set of elements that are present in the first set and not common in both sets. That means it removes the elements that exist in both sets. It does not return a new set, it just removes common elements from the first set. 

Syntax: setA.difference_update(setB)

  • Elements that exist in both setA and setB will be removed from setA.

intersection()

intersection method returns a set having elements that exist in all the specified sets. 

Syntax: set.intersection(set1, set2, … setn)

  • Set of elements that exist in the set, set1, set2, … setn will be returned.

issubset()

issubset method checks whether all elements of set A are present in set B or not. It returns a boolean value.

Syntax: setA.issubset(setB)

symmetric_difference()

This method returns a set containing elements from both the sets except those that are common in both sets. 

Syntax: setA.symmetric_difference(setB)

union()

union method returns a set containing all elements from both sets, except the duplicated ones.

Syntax: setA.union(setB)

Dictionary Commands

Dictionary is a built-in type in Python. It is used to store key-value pairs. It is ordered, modifiable, and does not allow duplicate keys. That means in a dictionary we can not add two pairs having the same value of keys. Python provides a set of built-in methods that we can use on dictionary objects. 

fromkeys(): fromkeys() method is used to generate a dictionary with specified keys and a specified value. 

Syntax: dict.fromkeys(keys, value)

  • keys is the tuple or list of key elements.
  • value refers to the value which would be paired with all the specified keys.

get()

get method is used to get a value of the specified key. If a key is not found in the dictionary it will return nothing unless we specify something in the parameters. 

Syntax: dictionary.get(key, value)

  • dictionary is the name of the dictionary object in which you want to search. 
  • key refers to the key that you want to search in the dictionary.
  • value is the value that would be returned if the key is not found in the dictionary.

items()

items method is used to display the dictionary elements. It will display all the key-value pairs present in the dictionary. It returns a view object which will contain all the key-value pairs as tuples in a list. It does not take any parameters.

Syntax: dictionary.items()

keys()

keys method is used to get all the keys present in the dictionary. It returns a view object containing all keys of the dictionary as a list. It does not take any parameters.

Syntax: dictionary.keys()

values()

values method is used to get all the values present in the dictionary. It returns a view object containing all values of the dictionary as a list. It does not take any parameters.

Syntax: dictionary.values()

pop()

pop method is used to remove a key-value pair from the dictionary by specifying the key. It returns the value of the key-value pair that is need to be removed. 

Syntax: dictionary.pop(key)

  • key refers to the key of the pair that you want to remove from the dictionary.

popitem()

popitem command is used to remove the last inserted pair from the dictionary. It does not take any parameters. It returns the removed pair as a tuple.

Syntax: dictionary.popitem()

setdefault()

setdefault method is used to get the value of a specified key. If the key does not exist it will insert the key with the value passed as a parameter. If you don’t specify any value it will insert the key with value None. 

Syntax: dictionary.setdefault(key, value)