Convert list to set Python
In this post, you will learn how to convert a list to a set using the Python programming language.
A list is an arranged collection of elements. It is used to store collections of data. It can contain a list of various types of data objects with a comma separated and enclosed within a square bracket. It is mutable, which means we can change the order of elements. Individual elements can be replaced even after the list has been created. This is one of the leading reasons why lists are being widely used. Lists play an effective role in small projects as they consume more memory.
A set is one of four built-in data types in Python used to store multiple items in a single variable. A set is created by using the built-in set() function, placing all the items inside the curly braces {}, separated by commas. Set items are unordered, unchangeable, and do not allow duplicate values.
Convert list to set using set() function
Python provides a built-in function set() that takes any iterable as an argument and converts it into a set object. In the given example, we have defined the list as x_list containing four data elements and passed the entire list as a parameter within the set() function. It will return the set data type as an output. At last, we have printed the set.
# Create a list
x_list = ['pizza','burger','cookies','milkshake']
# Convert the list using set
x_set = set(x_list)
#display the set
print(x_set)
Output of the above code:
{'burger', 'pizza', 'milkshake', 'cookies'}
Convert list to set using custom function
Here, we have defined a custom function to convert the list to a set. Inside this function, we create a set method object and iterate over the list, converting each element to a set.
# Convert list to set
def list_to_set(list1):
s = set()
for x in list1:
s.add(x)
return s
# Create a list
x_list = ['pizza','burger','cookies','milkshake', 'pizza']
# call function
x_set = list_to_set(x_list)
print(x_set)
Output of the above code:
{'milkshake', 'pizza', 'cookies', 'burger'}
Convert list to set using for loop
In the given Python program, we use the for loop to access each element of a list and add that element to a set by using the add() function.
# Create a list
x_list = ['pizza','burger','cookies','milkshake']
# defining set
x_set = set()
# converting list to set
for i in x_list:
x_set.add(i)
print(x_set)
Output of the above code:
{'milkshake', 'cookies', 'pizza', 'burger'}
Convert list to set using set comprehension
In the given Python program, we use the set comprehension to convert a list to a set.
# Create a list
x_list = [20,'Phy',50,'Che','Maths',90]
# converting list to set
# using set comprehension
t = {x for x in x_list}
print(t)
Output of the above code:
{'Che', 'Phy', 50, 20, 'Maths', 90}
Convert list to set using dict.fromkeys()
Python has a predefined fromkeys() method to create a new dictionary from a given sequence of elements. In the given Python program, we have used this method to convert a list to a set.
# Create a list
x_list = [22,'Phy',51,'Che','Maths',90]
# converting list to set
# using set comprehension
x = list(dict.fromkeys(x_list))
converted_set = set(x)
print(converted_set)
Output of the above code:
{90, 51, 22, 'Phy', 'Maths', 'Che'}
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary