Convert set to list Python
In this post, you will learn how to convert a set to a list using the Python programming language.
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.
set1 = {'burger', 'pizza', 'milkshake', 'cookies'}
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.
list1 = [10, 33, "Hello", 34]
Convert set into list using list() function
The Python list() function takes any iterable as a parameter and returns a list.
Syntax-list(iterable)
Here, the iterable can be a sequence (string, tuples) or a collection (set, dictionary) or any iterator object. In the given Python program, we use the list() method to convert a set into a list.
# Python program
# convert a set into a list
def convert_set_list(set):
return list(set)
# Driver function
x_set = set({'burger', 'pizza', 'milkshake', 'cookies'})
print(convert_set_list(x_set))
Output of the above code:
['burger', 'cookies', 'milkshake', 'pizza']
Convert set into list using sorted() function
Python provides a predefined function sorted() that returns a sorted list of the specified list. It accepts both object types, numbers and strings. Numbers are sorted numerically, and strings are sorted alphabetically.
Syntax-sorted(iterable, key, reverse)
In the given Python program, we use the sorted() method to convert the set into a list in a defined order.
# Python program
# convert a set into a list
def convert_set_list(set):
return sorted(set)
# Driver function
x_set = set({"car","bike","truck","motorcycle","train"})
print(convert_set_list(x_set))
Output of the above code:
['bike', 'car', 'motorcycle', 'train', 'truck']
Convert set into list by unpack set inside list
In this process, we unpack the set inside the list literal, which is created due to the presence of the single comma(,).
# Python program
# convert a set into a list
food_set = {'milkshake', 'pizza', 'cookies', 'burger'}
food_list = [*food_set, ]
print(food_list)
Output of the above code:
['milkshake', 'burger', 'pizza', 'cookies']
Convert set into list using for loop
Here, we create an empty list and manually add the elements of set to the list for converting the python set to list data structure with the help of for loop and append() method.
# Python program
# convert a set into a list
sample_set = {'milkshake', 'pizza', 'cookies', 'burger'}
l = []
for i in sample_set:
l.append(i)
print(l)
Output of the above code:
['pizza', 'cookies', 'burger', 'milkshake']
Convert a frozenset to a list
The frozenset() function returns a new frozenset object, optionally with elements taken from an iterable. It is an immutable, unordered data type. To convert a fronzenset into a list, we have to use the list() function and pass the set as a parameter to get the list object as an output.
Syntax:frozenset([iterable])
Here, iterable is an iterable object, like list, set, tuple, etc. In the given Python program, we convert a frozenset to a list.
x_set = frozenset({89, 32, 76, 23, 98})
x_list = list(x_set)
print(x_list)
Output of the above code:
[32, 98, 23, 89, 76]
Related Articles
Python Convert String to UppercaseConvert string to int Python
Python program to reverse a string
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Convert string to lowercase Python
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python iterate list with index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Strip function in Python
Python raise keyword
Check if two strings are anagrams Python
Python program to map two lists into a dictionary