Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»Difference between Set and List in Python
    python

    Difference between Set and List in Python

    March 27, 2023No Comments3 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Python’s Set vs. List Difference

    The article below compares and contrasts the Python collections Set and list. Python’s Data Structures, including sets and lists, are used to efficiently store and organise data.

    manner.

    List

    Python’s list functions similarly to other dynamic arrays, such as the vector array in C++ or the ArrayList array in Java. One of Python’s most useful tools is the list, which does not need its elements to have any common properties.

    The following are some of the most salient features of

    list:

    Case in point:

    • It is the datatype offered by Python. The users can write it as the list whose values can be separated by a comma and are written between the square brackets.
    • It can be converted into different data types, and the users can store any element of data in it. Therefore, the List is mutable.
    • It is ordered.

    Resulting



    1. # let’s see the Python code for demonstrating the List Data Structure.



    2. # First, we will create the List


    3. List


      = []


    4. print(“JavaTpoint data List: “)

    5. print(List)


    6. # we are creating the List of numbers


    7. List


      = [12, 24, 64, 18, 3, 201, 65, 35, 27, 29, 58, 42, 87, 30, 28, 79, 4, 90]


    8. print(“\n The JavaTpoint List of numbers: “)

    9. print(List)


    10. # Now, we will create the List of strings and will access it using index method.



    11. List


      = [“let’s”, “learn”, “Python”, “from”, “JavaTpoint”]


    12. print(“\nList Items: “)

    13. print(List[0])

    14. print(List[2])

    15. print(List[1])

    16. print(List[4])

    17. print(List[3])

    18. # Now we will check is list are ordered



    19. List1


      = [9, 3, 6, 19, 67, “Hey”, “JavaTpoint”, 78, 2, 1]



    20. List2


      = [9, 3, 6, 19, 67, 78, “Hey”, “JavaTpoint”, 2, 1]


    21. print(”

      List1


      = “, List1)


    22. print(”

      List2


      = “, List2)



    23. List1


      == List2

    :

    JavaTpoint data List: 
    []
     The JavaTpoint List of numbers: 
    [12, 24, 64, 18, 3, 201, 65, 35, 27, 29, 58, 42, 87, 30, 28, 79, 4, 90]
    List Items: 
    let's
    Python
    learn
    JavaTpoint
    from
    List1 =  [9, 3, 6, 19, 67, 'Hey', 'JavaTpoint', 78, 2, 1]
    List2 =  [9, 3, 6, 19, 67, 78, 'Hey', 'JavaTpoint', 2, 1]
    False
    

    Set

    Python’s sets are the changeable, iterable collection of data types that lack a natural ordering. No two items in a set may be the same. As compared to List, Python’s sets data storage tool has the benefit of providing more efficient ways for testing for the existence of individual elements.

    What follows is a list of salient features of

    set:

    • It is the unordered collection of items or data types in Python.
    • The order of elements stored in it is not fixed. The order of set elements can be changed.
    • A set of elements is defined between curly brackets { }.
    • Although only immutable elements are stored in sets, it is mutable.

    Illustration:

    Product Code:



    1. # let’s see the Python code for demonstrating the Set Data Structure.



    2. # First, we will create the Set


    3. set


      setA


      = set()


    4. print(“Intial JavaTpoint Set: “)

    5. print(setA)


    6. # we are creating the Set by using Constructor

    7. # we will use object for Storing String)


    8. String


      =


      ‘lets learn Python from JavaTpoint’





    9. set


      setA


      = set(String)


    10. print(“\n Storing the set by using the Object: ” )

    11. print(setA)


    12. # now, we will create the Set by using the list


    13. set


      setA


      = set([“let’s”, “learn”, “Python”, “from”, “JavaTpoint”])


    14. print(“\n Storing the set by using the List: “)

    15. print(setA)
    Intial JavaTpoint Set: 
    set()
     Storing the set by using the Object: 
    {'t', 'v', 'a', 'r', 'T', 'l', 'n', 'y', 'e', ' ', 'h', 'P', 'p', 'f', 'o', 's', 'i', 'J', 'm'}
     Storing the set by using the List: 
    {'JavaTpoint', "let's", 'learn', 'from', 'Python'}
    

    Lists vs. Sets

    Lists Sets
    Lists are Ordered. Sets are Unordered.
    Lists are Mutable. Sets are mutable but only stored immutable elements.
    Elements can be changed or replaced in Lists. Elements cannot be changed or replaced.
    Difference between Set and List in Python Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleUnderstanding the Python Mock Object Library
    Next Article Natural Language Processing With Pythons NLTK Package

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.