How To Convert List To Set
In this article, nosotros volition explore the list and set and conversion from the list to fix in python. Before jumping to this exciting affair, first of all, let's have an overview of the concept of lists and ready.
What is List in python?
Listing in python is defined as the ordered collection values of different data types inside a foursquare subclass [].
From the above image, nosotros tin can have an overview of the list. Here, 0,1 and two are the alphabetize. Alia, Meera, and Arya are the values or elements of the listing having 'cord' data blazon.
But the listing tin be Li = [0, 'Suman', 0.24, True]. The length of this list volition be 4, having the following indexes: – Li [0] = 0, Li [1] = 'Suman', Li [2] = 0.24, Li [iii] = True.
What is a set in python?
Set in python is divers as the unordered drove of unique items having unlike data types inside a curly bracket {}. For instance, Se = {1, 'Mandeep', True} or may exist Se = {'Sheetal', 'Karun'}.
Difference between list and set
Y'all may be wondering if both list and set can hold items having different information types than what is the deviation betwixt them?
Listing | Ready |
The list is an Ordered Collection of elements. | Set is an Unordered Collection of elements. |
Elements of the list can be modified and replaced. | Elements in a ready cannot exist altered, modified, or replaced. |
Till now you have understood the concepts of list and set. Simply the question is why exercise we demand this conversion from the list to set?
The answer is that set does not allow indistinguishable elements. If we employ a ready and so the elements volition be unique.
Why practice we need to remove duplicate elements?
we demand to remove duplicate elements because there are many instances when nosotros don't need indistinguishable values. Well, let me explain this with the help of an example. In real life whenever a user makes an entry into the database, there are high chances that he might commit a error while inbound data. Suppose a instructor is inbound the marks of students into an excel sheet, he ended upward entering a student proper name along with the marks and roll number twice into the excel sheet. So, practically we need to write some code to not let that happen. Hence, we can convert a list into a set.
List to set conversion
You lot have understood why we need to convert Listing to Set. Now permit's explore how to catechumen the list to a fix. There are many approaches to doing this.
i. Using ready() Function
This approach is 1 of the simplest methods of converting a list into a set. All y'all need is to employ the prepare() constructor and laissez passer the list as an statement.
Syntax: fix(listing).
#create a list my_list = ['Rice', 'Potato', 'Tomato plant'] #convert the list using set se = set(my_list) #display the set impress(se)
Explanation of the code
- Created a list having few elements.
- Converted the list to a set using set data structure by passing the listing as a parameter.
- Displayed the items in the prepare.
2. Using Custom Function
This approach is using a function phone call in python.
def list_to_set_conversion(list1): se = set up() for x in list1: se.add(x) return se Names = ['Alia', 'Bob', 'Ana', 'Sita', 'Alia'] s = list_to_set_conversion (Names) print(southward)
OUTPUT: {'Ana', 'Alia', 'Bob', 'Sita'}
3. Using dict.fromkeys()
The disadvantage of the above two approaches was that we were not getting the set up in an orderly manner. Then, for this, we are using our next arroyo to preserve the lodge.
list1 = ['Alia', 'Bobby', 'Bobby', 1, ane, 2, three] x = listing(dict.fromkeys(list1)) se = gear up(x) print(se)
Time Complexity of Converting List into a Set
Every algorithm and every lawmaking in this globe has a certain space and fourth dimension complexity. The time complication of converting a list to a gear up is linear i.e., the number of elements present in the list. Then, if the set has 'n' elements, the fourth dimension complexity is O(n). The reason behind this is that the loop will iterate over each chemical element present in the list that is O(due north), and add this element to the set volition toll O(ane). Together the fourth dimension complexity will be formulated equally O(due north) * O(1) = O(due north).
Likewise See
Conclusion
As we all know python is a very simple linguistic communication to sympathise considering of its simplicity. Due to this the conversion of the python list becomes simpler and easier to read. It'southward even elementary to understand at what cost nosotros can convert through the time complexity. The time complexity is O(n) which is minimal.
Summary
- List in python is ordered drove values.
- It can have any type of information.
- List is mutable.
- It can have indistinguishable elements.
- The element of the list can be accessed by its index.
- Sets are an unordered collection of elements.
- Sets are immutable.
- It cannot have duplicate elements.
- Gear up has a highly optimized method for checking whether an chemical element is contained in the listing.
- Prepare is based on the Hash table data structure.
- Elements in sets cannot be accessed by its index.
- A set up() method is used to convert the list into a set by simply passing the list equally the parameter.
- The fourth dimension complication for the conversion is O(due north) using a loop.
QNA
Let'south go through some questions to make our learning fun and interactive. We tin can add together a pocket-size practice afterwards every topic so that learners tin perform them to gain their conviction.
- Predict the output of the code?
def list_to_set_conversion(list1): se = set() for x in list1: se.add(x) return se Names = ['Tina', 'Kimmi', 'Chanda', 'Sita', 'Alia', 'Chanda', 'Tina'] s = list_to_set_conversion (Names) impress(southward)
Ans: –
Please leave a feedback on thisten
ii. Complete the missing part of the code then that it displays the post-obit as the correct output.
{'Abhishek', 'Ramesh', 'Mohan', 'John', 'Riya'}
names = ['Mohan', 'Abhishek', 'Ramesh', 'Mohan', 'John', 'Riya'] due south =? print(s)
Ans:-
Delight go out a feedback on thisx
3.Find the length of the given code snippet.
names = ['Mohan', 'Abhishek', 'Ramesh', 'Mohan', 'John', 'Riya', 'John'] print(len(names))
Ans: –
Please leave a feedback on this10
4. What is the time complication of the post-obit code snippet?
def list_to_set_conversion(list1): se = set() for x in list1: se.add(x) return se Names = ['Arunima', 'Bobita', 'Annam', 'Sita', 'Alia', 'Annam', 'Alia'] s = list_to_set_conversion (Names) print(due south)
Ans: –
Please leave a feedback on thisx
5. Find the length of the given code snippet.
names = {'Mohan', 'Abhishek', 'Ramesh', 'Mohan', 'John', 'Riya', 'John'} impress(len(names))
Ans: –
Delight leave a feedback on thisten
Source: https://www.pythonpool.com/convert-list-to-set-python/
0 Response to "How To Convert List To Set"
Post a Comment