Python

Convert Set to list Python

In Python, sets and lists both contain collections of objects, but there are significant differences between these two. If we talk about sets, they are valuable for executing a massive range of intersections and unions. On the other hand, lists provide those operations that help you to find duplicated items. In other words, a list contains identical items, while a set contains exceptional values. Another distinct quality is that the list uses square brackets, while the set uses curly brackets. Keep in mind that a dictionary in Python also uses curly braces. The common thing in sets and lists is that they hold strings, floats, integers, or other data types. So, if you have a set of elements in Python, you can also make a set of strings in python.

Example 1

In this example, we discuss the method for altering a list into a set in Python. The method is quite straightforward. For the list conversion process, we use a Spyder compiler and a source code file to know how it works. So, all you need to do is to create a blank file where you write your list conversion code.

First, you create the list by adding values to it. Use set() function for list conversion into a set. In this function, you fed the list name you wish to convert. So, in this program, we assign “set(country_names)” to change the list names into a set to a variable such as “country_names” and show the result using the print() function. The print function is equal to print(set(country_names)).

country_names = [‘UK’, ‘USA’, ‘Turkey’, ‘UAE’]
result = set( country_names)
print( ‘Result: ‘, result)

To check the output of the list conversion method, save your code file with the “.py” extension.

Then press F5 and run the program. The list is converted successfully to the set, and output is exhibited on the console screen.

Example 2

As we discussed earlier, a set is an unorganized group of items and values, while a list is organized and holds the same elements. If you convert a list into a set, it creates a new filtered set with duplicate elements, but sets don’t contain duplicates. You can’t replace, modify, or alter elements in the set. This method uses a custom function call. We use this method in the same code file, “ListintoSet”.

In this program, we first define a list, call a set() function, and assign it to a variable. Then we use a for loop that traverses over the list and stores the result in the variable. Then we create a list of students. After that, we use the list command that we defined earlier, pass the “student_names” list as an argument, and assign it to a variable. Then we use a print command which displays the output of the conversion list.

def list_to_set_conversion(my_list)
    res = set()
    for x in my_list
        res.add(x)
    return res
Student_Names = [‘Julie’, ‘Bob’, ‘John’, ‘Sita’, ‘Ana’]
s= list_to_set_conversion(Student_Names)
print(s)

Ctrl+S (save) and F5 (run) the code file and check how the python list converts into a set:

Example 3

In our third example code, we use the dict.fromkeys() method by using Spyder compiler and the same code file “ListintoSet”. Using this approach, we not just convert the list into a set but also well-organized elements in the set. To reserve the order, we use dict.fromkeys() method.

In this program, we first create a list with student names and rollno. Then we call a dict.fromkeys() function and pass “names_rollno” list as an argument, and assign it to a variable. Then we call a set() function, which takes the “res” variable as a parameter and assigns it to a variable “x”. Then the print command displays the serialized set stored in the variable “x”.

name_rollno = [‘John’, ‘Anna’, ‘Anna’, 1, 2, 2, 3]
res = list(dict.fromkeys(name_rollno))
x = set(res)
print(x)

To check the output, save and run the program file “ListintoSet” and check how the dict.fromkeys() function works.

Conclusion

Python is a very simple and handy language. Here we discussed three list conversion methods that become easier to code and simpler to understand. I hope you will get full assistance while working on this type of code.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content