banner



How To Convert List To Set In Python

In this commodity, we will explore the list and set up and conversion from the list to set in python. Before jumping to this exciting thing, get-go of all, let'southward have an overview of the concept of lists and set.

What is List in python?

List in python is divers as the ordered collection values of different data types inside a square bracket [].

What is List in python
List in python

From the to a higher place epitome, we tin can have an overview of the list. Here, 0,1 and 2 are the alphabetize. Alia, Meera, and Arya are the values or elements of the list having 'cord' information type.
But the listing tin be Li = [0, 'Suman', 0.24, True]. The length of this list will exist four, having the following indexes: – Li [0] = 0, Li [1] = 'Suman', Li [ii] = 0.24, Li [3] = True.

What is a ready in python?

Set in python is defined every bit the unordered collection of unique items having different data types inside a curly bracket {}. For example, Se = {one, 'Mandeep', True} or may be Se = {'Sheetal', 'Karun'}.

set in python
Sets in python

Difference between list and set

You lot may be wondering if both list and gear up can concur items having dissimilar data types than what is the difference between them?

List Set up
The list is an Ordered Collection of elements. Set is an Unordered Drove of elements.
Elements of the list tin be modified and replaced. Elements in a set cannot exist altered, modified, or replaced.

Till now you have understood the concepts of list and set. But the question is why do we need this conversion from the listing to set?
The answer is that gear up does not allow duplicate elements. If we employ a set and then the elements will be unique.

Why do we demand to remove duplicate elements?

we demand to remove duplicate elements because there are many instances when we don't need duplicate values. Well, let me explicate 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 mistake while entering data. Suppose a teacher is entering the marks of students into an excel sheet, he concluded up entering a student name along with the marks and gyre number twice into the excel sail. So, practically nosotros demand to write some code to non let that happen. Hence, we can catechumen a list into a set.

Listing to set conversion

Y'all have understood why we need to convert List to Gear up. Now let's explore how to convert the listing to a ready. At that place are many approaches to doing this.

ane. Using prepare() Office

This arroyo is one of the simplest methods of converting a list into a set. All yous need is to utilise the prepare() constructor and pass the list as an statement.

          Syntax:          ready(list).
#create a list my_list = ['Rice', 'Potato', 'Tomato']  #convert the listing using fix se = set up(my_list)  #display the set print(se)          

Explanation of the code

  1. Created a list having few elements.
  2. Converted the list to a prepare using ready data construction by passing the list every bit a parameter.
  3. Displayed the items in the fix.

2. Using Custom Part

This approach is using a function call in python.

def list_to_set_conversion(list1):     se = ready()     for x in list1:         se.add together(10)     return se Names = ['Alia', 'Bob', 'Ana', 'Sita', 'Alia'] southward = list_to_set_conversion (Names) print(s)          
          OUTPUT:          {'Ana', 'Alia', 'Bob', 'Sita'}

3. Using dict.fromkeys()

The disadvantage of the above 2 approaches was that we were not getting the set in an orderly manner. So, for this, we are using our next approach to preserve the club.

list1 = ['Alia', 'Bobby', 'Bobby', 1, i, 2, three] ten = list(dict.fromkeys(list1)) se = set up(x) impress(se)          
Using dict.fromkeys()
dict.fromkeys()

Time Complexity of Converting List into a Set

Every algorithm and every lawmaking in this world has a certain space and time complexity. The time complexity of converting a list to a set is linear i.e., the number of elements nowadays in the list. And then, if the set has 'due north' elements, the fourth dimension complexity is O(north). The reason behind this is that the loop will iterate over each chemical element nowadays in the list that is O(northward), and add this element to the set will cost O(1). Together the time complexity will be formulated equally O(n) * O(1) = O(n).

Also Run across

Determination

As we all know python is a very elementary linguistic communication to understand because of its simplicity. Due to this the conversion of the python listing becomes simpler and easier to read. Information technology's fifty-fifty simple to understand at what cost we can convert through the fourth dimension complication. The time complication is O(due north) which is minimal.

Summary

  1. List in python is ordered drove values.
  2. It can accept any type of information.
  3. List is mutable.
  4. It can have duplicate elements.
  5. The element of the list tin can be accessed past its index.
  6. Sets are an unordered collection of elements.
  7. Sets are immutable.
  8. It cannot take duplicate elements.
  9. Set has a highly optimized method for checking whether an element is contained in the list.
  10. Set is based on the Hash table data structure.
  11. Elements in sets cannot exist accessed by its index.
  12. A fix() method is used to catechumen the listing into a set by simply passing the list as the parameter.
  13. The fourth dimension complexity for the conversion is O(due north) using a loop.

QNA

Let's get through some questions to make our learning fun and interactive. Nosotros can add a small exercise afterward every topic so that learners can perform them to gain their confidence.

  1. Predict the output of the code?
def list_to_set_conversion(list1):    se = set()     for x in list1: se.add(x)     render se Names = ['Tina', 'Kimmi', 'Chanda', 'Sita', 'Alia', 'Chanda', 'Tina'] s = list_to_set_conversion (Names) print(south)          

Ans: –

0

Please exit a feedback on thisten

two. Complete the missing part of the code so that it displays the following as the correct output.

{'Abhishek', 'Ramesh', 'Mohan', 'John', 'Riya'}

names = ['Mohan', 'Abhishek', 'Ramesh', 'Mohan', 'John', 'Riya'] due south =? print(due south)          

Ans:-

0

Delight leave a feedback on thisx

3.Discover the length of the given code snippet.

names = ['Mohan', 'Abhishek', 'Ramesh', 'Mohan', 'John', 'Riya', 'John'] impress(len(names))          

Ans: –

0

Please leave a feedback on thisx

4. What is the fourth dimension complexity of the following lawmaking snippet?

def list_to_set_conversion(list1):     se = set()     for x in list1:         se.add(x)     render se Names = ['Arunima', 'Bobita', 'Annam', 'Sita', 'Alia', 'Annam', 'Alia'] due south = list_to_set_conversion (Names) print(s)          

Ans: –

0

Delight exit a feedback on thisx

5. Discover the length of the given lawmaking snippet.

names = {'Mohan', 'Abhishek', 'Ramesh', 'Mohan', 'John', 'Riya', 'John'} print(len(names))          

Ans: –

0

Please leave a feedback on thisx

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

0 Response to "How To Convert List To Set In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel