banner



How To Make A List Into A Set

Convert List to Set in Python

  1. Difference Between a Listing and a Set
  2. Using the set() Method to Convert the Listing to Prepare in Python

This tutorial demonstrates how to convert a list into a set in Python.

Difference Between a Listing and a Set

Lists and sets are standard Python data types that store values in sequence.

A Python List stores comma-separated values between square brackets. It'south mutable, which means that its items can be modified or changed. The affair nearly the Python Listing is that the items do not need to exist of the aforementioned blazon.

Below is an example of what a Python List looks like:

              list_1 = [1, two, iii] list_2 = ['Milk', 'Bread', 'Eggs'] # List with multiple information types list_3 = ['Chocolate', 15.25, 200]                          

Python sets are an unordered list of unique elements that may have unlike data types. The object stores comma-separated values and is encapsulated in curly braces {}. It is iterable and mutable, even so information technology only stores immutable objects; this ways that a fix itself can be modified — we can add together or remove elements; nonetheless, the elements within the set must not modify. We cannot modify or admission the elements within a set using slicing or indexing.

Here's an example of what a Python Gear up looks like:

              set_1 = {1, two, 3} set_2 = {'Milk', 'Staff of life', 'Eggs'} # Sets with multiple data types set_3 = {17.0, 'Hello Earth', (ten, 20, thirty)}                          

Now, we know the differences between a Python Listing and a Gear up, let'south dive into converting a Listing into a Set.

Using the prepare() Method to Convert the List to Set in Python

The simplest mode to convert a listing to a prepare is by using Python'due south born method, the ready() method. Converting a List with duplicate values volition be automatically removed by the gear up() method and will retain the aforementioned unique elements equally the initial List. The set() method only accepts one parameter every bit an argument:

*iterable (optional) - a sequence (string, list, tuple, etc.) or collections of elements

Here's the syntax:

              set(iterable)                          

The set() method returns:

  • an empty set, if no arguments is passed.
  • a constructed fix

Case 1: Convert a Python List with Unique Values

              # initialize a list _list = [5, 4, 3, 1, half dozen, two]  # convert list to fix converted = set(_list)  impress(converted)                          

Output:

              {ane, 2, 3, 4, 5, half-dozen}                          

Example 2: Catechumen Listing with Duplicate Values

              # initialize a listing grocery_list = ["milk", "staff of life", "eggs", "bread", "hotdogs", "eggs"]  # catechumen list to set converted = set(grocery_list)  print(converted)                          

Output:

              {'eggs', 'breadstuff', 'milk', 'hotdogs'}                          

Every bit you lot can run into, the converted variable now only contains unique values.

Instance 3: Catechumen List with Mixed Information Types

              # initialize a listing with mixed data types grocery_list = ["milk", "bread", "eggs", ("Ham and Cheese", 2000), 19.43]  # convert the grocery_list to set grocery_set = fix(grocery_list)  # display the converted grocery_list print(grocery_set)                          

Output:

              {'milk', 'bread', 'eggs', ('Ham and Cheese', 2000), 19.43}                          

Python sets provide an array of methods you can choose from depending on your use-case, and non simply convert a list into a set. There are set methods used to perform mathematical operations: union, intersection, difference, and symmetric difference. We tin can achieve this past using some defined Python set operators and methods.

Write for us

DelftStack articles are written past software geeks like you. If y'all also would similar to contribute to DelftStack by writing paid articles, yous can check the write for us page.

Related Commodity - Python Set

  • Join 2 Sets in Python
  • Add Values to a Set in Python
  • Check if a Set up Is a Subset of Another Prepare in Python
  • Catechumen Gear up to String in Python

    Related Article - Python List

  • Join 2 Sets in Python
  • Add Values to a Set in Python
  • Bank check if a Prepare Is a Subset of Another Set in Python
  • Catechumen Set to String in Python
  • Ezoic

    Source: https://www.delftstack.com/howto/python/python-list-to-set/

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

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel