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
-
# let’s see the Python code for demonstrating the List Data Structure.
-
-
# First, we will create the List
-
List
= []
-
print(“JavaTpoint data List: “)
-
print(List)
-
-
# we are creating the List of numbers
-
List
= [12, 24, 64, 18, 3, 201, 65, 35, 27, 29, 58, 42, 87, 30, 28, 79, 4, 90]
-
print(“\n The JavaTpoint List of numbers: “)
-
print(List)
-
-
# Now, we will create the List of strings and will access it using index method.
-
-
List
= [“let’s”, “learn”, “Python”, “from”, “JavaTpoint”]
-
print(“\nList Items: “)
-
print(List[0])
-
print(List[2])
-
print(List[1])
-
print(List[4])
-
print(List[3])
-
# Now we will check is list are ordered
-
-
List1
= [9, 3, 6, 19, 67, “Hey”, “JavaTpoint”, 78, 2, 1]
-
List2
= [9, 3, 6, 19, 67, 78, “Hey”, “JavaTpoint”, 2, 1]
-
print(”
List1
= “, List1)
-
print(”
List2
= “, List2)
-
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:
-
# let’s see the Python code for demonstrating the Set Data Structure.
-
-
# First, we will create the Set
-
set
setA
= set()
-
print(“Intial JavaTpoint Set: “)
-
print(setA)
-
-
# we are creating the Set by using Constructor
-
# we will use object for Storing String)
-
String
=
‘lets learn Python from JavaTpoint’
-
set
setA
= set(String)
-
print(“\n Storing the set by using the Object: ” )
-
print(setA)
-
-
# now, we will create the Set by using the list
-
set
setA
= set([“let’s”, “learn”, “Python”, “from”, “JavaTpoint”])
-
print(“\n Storing the set by using the List: “)
-
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. |