-
Ordered Collection:
- Lists are ordered collections of elements. The order in which elements are added is preserved.
- Elements can be accessed by their index.
my_list = [1, 2, 3, 4, 5] print(my_list[0]) # Output: 1
-
Mutable:
- Lists are mutable, meaning you can modify their elements after creation.
my_list[1] = 10
-
Allows Duplicate Elements:
- Lists can contain duplicate elements.
my_list = [1, 2, 2, 3, 4]
-
Use Cases:
- Use lists when you need an ordered collection with the ability to modify elements.
-
Unordered Collection:
- Sets are unordered collections of unique elements. The order in which elements are added is not preserved.
- Elements cannot be accessed by their index.
my_set = {1, 2, 3, 4, 5}
-
Mutable:
- Sets are mutable, meaning you can add and remove elements after creation.
my_set.add(6)
-
No Duplicate Elements:
- Sets do not allow duplicate elements. If you try to add a duplicate, it won't raise an error, but the set won't change.
my_set = {1, 2, 2, 3, 4} # Results in {1, 2, 3, 4}
-
Use Cases:
- Use sets when you need an unordered collection of unique elements, and you want to perform set operations like union, intersection, and difference.
-
Adding Elements:
- Lists use
append()orinsert()methods. - Sets use
add()method.
- Lists use
-
Removing Elements:
- Lists use
remove(),pop(), ordelstatement. - Sets use
remove()ordiscard()methods.
- Lists use
-
Checking Membership:
- Lists use the
inoperator. - Sets use the
inoperator as well, which is more efficient for sets.
- Lists use the
# Lists
if 3 in my_list:
print("3 is in the list")
# Sets
if 3 in my_set:
print("3 is in the set")-
Use Lists When:
- You need to maintain the order of elements.
- Duplicate elements are allowed.
- You need to access elements by index.
-
Use Sets When:
- Order doesn't matter.
- You want to ensure unique elements.
- You need to perform set operations like union, intersection, or difference.