Set to list in Python

Set to list in Python

In this article, we will discuss how we can convert a set to a list in Python.

Before that let’s have a quick revision of lists and sets.

List – It is a sequence of elements enclosed in square brackets where each element is separated with a comma.

Syntax of a list is-

We can print the list and check its type using-

NOTE: The list is mutable which means we can change its elements.

Set – It is an unordered collection of elements that contains all the unique values enclosed within curly brackets.

Syntax of a set is-

We can print the set and check its type using-

The different approaches of converting a set to a string that we will use are-

  1. Using list()
  2. Using sorted()
  3. Using *set
  4. Using for loop
  5. Using frozenset

Using list()

In the first method, we will use list() to convert the set.

The following program shows how it can be done-

Output:

['C','C++','Java','Python','HTML' ]

Explanation:

Let us understand what we have done in the above program-

  1. The first thing that we have done here is to declare the set that consists of different subject names.
  2. After this, we have used list() function in which we passed the set ‘subjects’.
  3. On executing the program, the desired output is displayed.

Using sorted()

The second approach is to use the sorted() function to convert a set to a list.

The program below illustrates the same-

Output:

['C','C++','Java','Python','HTML' ]

Explanation:

Let us understand what we have done in the above program-

  1. The first thing that we have done here is, we created a function that takes a set as its parameter and returns the expected output.
  2. After this, we have declared the variable of a set type that consists of different subject names.
  3. The next step was to pass our set in the function ‘convert_set’.
  4. On executing the program, the desired output is displayed.

Using *set

In the third method, we will use the *set to convert a set to a list in Python.

The *set unpacks the set inside a list.

The following program shows how it can be done-

Explanation:

Let us understand what we have done in the above program-

  1. The first thing that we have done here is, we created a function that takes a set as its parameter and returns the expected output.
  2. After this, we have passed the value of the set that consists of different subject names inside the set().
  3. The next step was to pass our set in the function ‘convert_set’.
  4. On executing the program, the desired output is displayed.

Output

['C','C++','Java','Python','HTML' ]

Using for loop

In the fourth method, we will use for loop to convert a set to a list in Python.

The program below illustrates the same-

Output:

['C','C++','Java','Python','HTML' ]

Let us understand what we have done in the above program-

  1. The first thing that we have done here is to declare the set that consists of different subject names.
  2. After this, we have declared an empty list res.
  3. We have used for loop here, that took each element from the set and added it to the list.
  4. On executing the program, the desired output is displayed.

Using frozenset

Finally, in the last method, we will use frozenset to convert a set to a list in Python.

The difference between a set and a frozenset is that a set is mutable whereas a frozenset is immutable.

The following program shows how it can be done-

Output:

['C','C++','Java','Python','HTML' ]

Explanation:

Let us understand what we have done in the above program-

  1. The first thing that we have done here is to declare the frozenset that consists of different subject names.
  2. After this, we have used list() in which we passed the set ‘subjects’.
  3. On executing the program, the desired output is displayed.

Conclusion

In this tutorial, we came across the different approaches of converting a set to a list in Python.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263397.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论