A set is an Unordered grouping of items. Each item of the set is distinct (unique) with no duplicates. You can’t directly change items in sets like in lists and dictionaries but you can add and delete items. So you can say that items in the dictionary are changeable. This article will teach us about python sets and dictionaries with related examples.

Sets

have the following properties:

. Represented by {} brackets

. Unordered – items do not have any specific order

. Unchangeable – items cannot be changed after creating the set

Example

s = {"banana", "apple", "grapes"}

Type of Set

s = {1,2,3,4,5}
print(type(s))

Output:
<class 'set'>

Duplicates in set

Sets cannot contain two items of the same value. Let’s check the output if we repeat elements in the set.

s = {1,2,2,3,4,4,5}
print(s)


Output:
{1,2,3,4,5}

Length of set

Python provides a built-in method to get the length of the set. It returns the total count of set elements.

s = {"Jan", "Feb", "Mar"}

print(len(s))


Output
3

Data types

The items in the set can be of any time such as string, number, or boolean.

s1 = {"Jan", "Feb", "Mar"}
s2 = {3, 2, 1, 5, 9}
s3 = {F, False, False}

A set can also contain items of different types.

set1 = {"xyz", 13, False, 30, "Pakistan"}

Set Constructor

We can also create a set in python using a set constructor. See the following example.

s = set(("Jan", "Feb", "Mar"))
print(s)

Dictionary

Python dictionary is a data structure that stores the data in the form of key:value pairs. Dictionary data structure possesses some properties that must be remembered before moving forward.

1. Items have a specific order and this order is not changed after creating the dictionary.

2. Items are changeable – we can add, remove and change items in the dictionary after creating it.

3. Duplicated items are now allowed

Example

Dictionary is created using curly brackets and contains keys and values. A simple example of a dictionary in python is given below.

d = {
  "name": "Jawad",
  "age": 2,
  "gender": "Male"
}

Note: By unordered items, it means that we cannot refer to an item using an index. In python versions 3.6 and below, dictionaries were unordered. But, from version 3.7 dictionaries are ordered.

Duplicates in dictionary

Duplicate values are replaced by a single entry. Let’s take an example.

d = {
  "name": "Merhawi",
  "age": 25,
  "age": 26,
  "gender": "Male"
}
print(d)

Output
{
  "name": "Merhawi",
  "age": 2,
  "gender": "Male"
}

Length of dictionary

The len() method returns the length of dictionary by counting the number of items in it.

d = {
  "name": "Merhawi",
  "age": 25,
  "gender": "Male"
}
print(d)


Output
3

Data types

A dictionary can contain items of type int, string, list, and boolean. The following example contains items from all of these data types.

d = {
  "name": "JMerhawi",
  "Male": True,
  "age": 25,
  "hobbies": ["Reading", "Mobile", "Programming"]
}

Tip: You can check if an object is of type dictionary using type function. It returns following output for a dictionary.

<class 'dict'>

Add elements

There are multiple ways to add an element to the dictionary. We can specify the key in the name of the dictionary and assign some value to it or specify the index and assign some value.

Adding one element at a time

d[0] = 'I'
d[2] = 'love'
d[3] = 'Eritrea'

print("Dictionary: ")
print(d)

Adding set to a single value

d['v1'] = '1, 2, 3'
print("Dictionary: ")
print(d)

Update elements

To update the value of an element in the dictionary, we can either specify its index or the name of the key and assign the new value. If that index or key exists in the dictionary, it will be updated with the new value. Otherwise, it will add a new element.

d[2] = 'care'

print("Dictionary: ")
print(d)

Access elements

To access the specific item of the dictionary, we can specify the dictionary name along with the name of the key in square brackets or its index.

d = {1: 'Orange', 'name': 'Merhawi', 3: 'Eritrea'}
  
print(d['name'])

print(d[1])

So, this article presents you the fundamentals of set and dictionary in python. You must practice these data structures on your own before moving to the next articles.

Categorized in: