Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Set from dictionary values

Python – Set from dictionary values

Given a dictionary, the task is to write a Python program to get all the values and convert to set.

Examples:

Input : test_dict = {‘Gfg’ : 4, ‘is’ : 3, ‘best’ : 7, ‘for’ : 3, ‘geek’ : 4}

Output : {3, 4, 7}

Explanation : 2nd occurrence of 3 is removed in transformation phase.

Input : test_dict = {‘Gfg’ : 4, ‘is’ : 3, ‘best’ : 7, ‘geek’ : 4}

Output : {3, 4, 7}

Explanation : 2nd occurrence of 4 is removed in transformation phase.

Method #1 : Using generator expression + {}

In this, we perform task of getting all the values using generator expression and {} operator performs task of removing duplicate elements and conversion to set.

Python3




# Python3 code to demonstrate working of
# Set from dictionary values
# Using generator expression + {}
 
# initializing dictionary
test_dict = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# {} converting to set
res = {test_dict[sub] for sub in test_dict}
 
# printing result
print("The converted set : " + str(res))


Output:

The original dictionary is : {‘Gfg’: 4, ‘is’: 3, ‘best’: 7, ‘for’: 3, ‘geek’: 4}

The converted set : {3, 4, 7}

Method #2 : Using values() + set()

In this, we perform task of getting values from dictionary using values() and set() is used to conversion to set.

Python3




# Python3 code to demonstrate working of
# Set from dictionary values
# Using values() + set()
 
# initializing dictionary
test_dict = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# values() used to get values
res = set(test_dict.values())
 
# printing result
print("The converted set : " + str(res))


Output:

The original dictionary is : {‘Gfg’: 4, ‘is’: 3, ‘best’: 7, ‘for’: 3, ‘geek’: 4}

The converted set : {3, 4, 7}

Method #3:Using Counter() function

Python3




# Python3 code to demonstrate working of
# Set from dictionary values
from collections import Counter
 
# initializing dictionary
test_dict = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# values() used to get values
freq = Counter(test_dict.values())
 
reslist = list(freq.keys())
 
res = set(reslist)
# printing result
print("The converted set : " + str(res))


Output

The original dictionary is : {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
The converted set : {3, 4, 7}

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #4: Using a loop to iterate over the dictionary values

Initialize an empty list called values_list.
Use a loop to iterate over the values of the dictionary test_dict.
Append each value to the values_list.
Convert the values_list to a set using the set() function and assign it to a variable called res.
Print the converted set res.

Python3




# Python3 code to demonstrate working of
# Set from dictionary values
 
# initializing dictionary
test_dict = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using loop to get values
values_list = []
for val in test_dict.values():
    values_list.append(val)
 
res = set(values_list)
 
# printing result
print("The converted set : " + str(res))


Output

The original dictionary is : {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
The converted set : {3, 4, 7}

Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary space: O(n), where n is the number of values in the dictionary.

RELATED ARTICLES

Most Popular

Recent Comments