| by Arround The Web | No comments

Convert Set to List Python

Python offers many built-in functionalities, such as “Sets” and “Lists”. These are collections of items that can store different types of data, but they have some important differences that affect how they can be used and manipulated. A “Set” is an unordered collection of unique items that do not allow duplicates and are mutable, meaning that they can be changed after they are created. A “List” is an ordered collection of items that allows duplicates and is also mutable, indicating that they can be changed after they are created.

Converting a set to a list in Python can be useful for various purposes, such as accessing, sorting, concatenating, or passing the items to a function that expects a list. To convert the given set to a Python list, various methods are used. Some of the methods are given below:

Method 1: Convert Set to a Python List Utilizing the “list()” Function

One of the simplest and easiest ways to convert a set to a specified list is by utilizing the “list()” function. This function makes a new list from all the items/elements of a particular iterable object.

Syntax

list(iterable)

 

Example

The following code uses the “list()” function to convert a set to a list:

set_value = {"Joseph", "Lily", "Anna", "Jon"}
list_value = list(set_value)
print(list_value)
print(type(list_value))

 

In the above code, the “list()” function takes the defined “set” as an argument and converts it into the list.

Output

In the above output, the set has been converted into the list appropriately.

Method 2: Convert Set to a Python List Utilizing the “for” Loop

Another way to convert a given set to a list is by utilizing the “for” loop. This loop iterates over an iterable object and executes/runs a block/piece of code for each element. It can be applied to append each item in the set to an empty list and retrieve the final list.

Syntax

The syntax of using a “for” loop to convert a set to a list is as follows:

new_list = []
for item in set:
  new_list.append(item)

 

Example

The following example code uses a “for” loop to convert a set to a list:

set_value = {"Joseph", "Lily", "Anna", "Jon"}
list_value = []
for i in set_value:
  list_value.append(i)
print(list_value)
print(type(list_value))

 

In the above code block, the “for” loop is used to iterate along the set and append each element of the set to the empty list via the “append()” method.

Output

A list has been created from the set in the above output accordingly.

Method 3: Convert Set to a Python List Utilizing “List Comprehension”

A third way to convert a set to a Python list is by utilizing the “List Comprehension” approach. This approach is a concise and expressive way of making a unique list from a current iterable object. The formula consists of an expression headed by a “for” clause and optionally another “if” clause.

Syntax

new_list = [expression for item in set if condition]

 

Example

Go through the below-stated code snippet:

set_value = {"Joseph", "Lily", "Anna", "Jon"}
list_value = [elem for elem in set_value]
print(list_value)
print(type(list_value))

 

Here, the “List Comprehension” approach is applied to iterate through the set and return a list containing each element of the set and return the “type” as well via the “type()” function.

Output

In this output, a list has been created from the set and the type is also returned.

Conclusion

To convert a set to a Python list in a program, apply the “list()” function, a “for” loop, or the “List Comprehension” approach. This write-up demonstrated the approaches to converting a set to a list in Python with the help of different examples.

Share Button

Source: linuxhint.com

Leave a Reply