banner



How To Turn A List Into A Set

Ezoic written report this ad

Ezoic report this ad

How to Convert Listing to Set Python

convert list to set python

In this article, nosotros will be solving a problem statement for how to convert List to Gear up Python. We will exist discussing the steps and python methods required to solve this trouble and also the time complexity of the methods used to convert list to gear up in python.

Before jumping directly into the python program permit's know about the background of the problem statement, python list, and python ready.

What is List in Python? The listing is one of the 4 built-in information types in Python used to store data that is mutable, tin can be ordered, and can be duplicated inside the list. The list is created by placing all the elements within the square brackets and separated past commas.

Case :              list1 = [1,ii,three,'john',seven.v,3]            

What is Set up in Python?Like list, Set up is also 1 of the iv built-in information types in Python used to store data just cannot have indistinguishable elements and cannot be modified. The Set up is created by placing all the elements inside the curly brackets separated by commas.

Case :              set1 = {1,two,3,'john',seven.v}            

Since now we know that set contains merely distinct elements, hence when we create gear up from listing nosotros will get all distinct element of a listing in the set .

Method 1- Using ready() to convert list to ready Python

The simplest fashion to catechumen list to prepare in Python is by using the fix() part. The set() method is used to convert an iterable element such as a list, dictionary, or tuple into the prepare.

Python Lawmaking:

              # sample_list is defined listing sample_list = [1,2,iii,'seeker',3,vii.five]  # set() to convert list to set sample_set  = set(sample_list) print(sample_set) #printing prepare            

Output:
{1, 2, three, vii.5, 'seeker'}

Method ii- For Loop to convert list to set Python

In this method, we use for loop to access each element of a list and add that chemical element in a prepare by using add() function.

Python Code

              # sample_list is defined listing sample_list = [i,2,three,'seeker',3,vii.5]  # gear up() to catechumen listing to set sample_set = set() # defining ready #using for loop for i in sample_list:     #adding i to b     sample_set.add(i) print(sample_set)            

Output:
{1, 2, 3, 7.five, 'seeker'}

Method 3- Using Set Comprehension to convert list to set Python

In this method, we will be using set comprehension. Y'all can know about fix comprehension from here.

Python Code

              # a is divers list a = [1,ii,three,'seeker',iii,7.v]  t = {ten for x in a} # set comprehension impress(t)            

Output:
{i, 2, 3, 7.five, 'seeker'}

Method 4- Using dict.fromkey() convert listing to set Python

In this method, we will be using the dictionary fromkeys() method. To know about dict.fromkey() method you can visit hither.
The only disadvantage of this method is we don't go set in an orderly mode.

Python Code

              # a is defined list a = [1,2,iii,'seeker',three,vii.5]  # Using dict.fromkeys() method ten = listing(dict.fromkeys(a)) converted_set = gear up(x) impress(converted_set)            

Output:
{1, 2, three, 7.five, 'seeker'}

Time Complexity to convert list to set in Python?

Fourth dimension complexity to convert listing to set up is Big O(North) , where Due north is the number of elements nowadays in the listing, i.e it is linear to the elements present in the listing. This is because the complexity of ready operations to add/delete an element is O(ane). Hence while converting a list of Due north elements into a set, we require such Northward operations, which volition have the complexity of Due north * O(1)  i.east O(Northward * one) = O(Northward).

In a higher place, nosotros accept discussed 4 methods that can help you to catechumen list to set in python. But which method is all-time from to a higher place with respect to time then that you lot can practice using that method in upcoming projects or problems.

Below is the table where you lot bank check the execution time of each method.

Method Proper name Time of Execution
Using ready() Function 0.00016019
Using for loop 0.00019580
Using Set Comprehension

0.00018290

From the above tabular array, we tin run into that using the set() function method takes less fourth dimension compared to other methods.

Conclusion

Hence we tin convert list to set up Python by using set function, for loop, and set comprehension. The Time complexity for converting the list to set up is O(Due north). We can likewise conclude the best way to convert listing to set is by using the set() method.

Most Author

Siddhesh Jadhav

I am Passionate Computer Engineer. Writing articles nearly programming problems and concepts allows me to follow my passion for programming and helping others.

Ezoic study this advertizing

Source: https://myprogrammingtutorial.com/convert-list-to-set-python/

0 Response to "How To Turn A List Into A Set"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel