Difference Between List & Set in Python

 

Hi! This short tutorial will explain to you the differences between list and set in the Python programming language.

First, though, here is an overview:

Let’s get right into the discussion and Python code!

 

Python List

A Python list is used to store many items, which could be in different data types, like strings, integers, in a single variable. Python lists are mutable, which means they can be altered or modified. Below are examples of Python lists containing integers, strings, and user-defined objects:

# list of integers
int_list = [1,2,3,4,5,6,7]
print(int_list)
 
# [1, 2, 3, 4, 5, 6, 7]
 
 
 
# list of strings
str_list = ["tangelo", "guava", "orange", "pear", "duran"]
print(str_list)
 
# ['tangelo', 'guava', 'orange', 'pear', 'duran']
 
 
 
# user-defined object class
class Result:
  def __init__(self, student, score):
    self.student = student
    self.score = score
 
# list of objects
my_list = []
my_list.append(Result("Jack",75))
my_list.append(Result("Mary",80))
my_list.append(Result("Harry",85))
my_list.append(Result("Kelly",79))
my_list.append(Result("Max",70))
 
# for loop to print object list
for l in my_list:
  print(l.student, l.score, sep = ",")
 
# Jack,75
# Mary,80
# Harry,85
# Kelly,79
# Max,70

We can also create a Python list using the list() function:

# list of integers
int_list = list((1,2,3,4,5,6,7))
print(int_list)
 
# [1, 2, 3, 4, 5, 6, 7]
 
 
# list of strings
str_list = list(("tangelo", "guava", "orange", "pear", "duran"))
print(str_list)
 
# ['tangelo', 'guava', 'orange', 'pear', 'duran']
 
 
# user-defined object class
class Result:
  def __init__(self, student, score):
    self.student = student
    self.score = score
 
# list of objects
my_list = list((Result("Jack",75), Result("Mary",80), Result("Harry",85), Result("Kelly",79), Result("Max",70)))
 
# for loop to print object list
for l in my_list:
  print(l.student, l.score, sep = ",")
 
# Jack,75
# Mary,80
# Harry,85
# Kelly,79
# Max,70

In the examples above, we parsed a tuple to the list constructor, which returned a list of integers, strings and user-defined objects.

 

Attributes of Python List

Below are the attributes of Python lists:

  • A list is an ordered collection of elements, where each element has a specific index starting from 0.
  • Lists are mutable, meaning you can add, remove, or modify elements after creating the list.
  • The elements in a list can be accessed by their index values.
  • The indexes of a list are always integers.
  • Lists are iterable, which means you can iterate over the elements in a list with a for loop or other iterable methods
  • A list can hold duplicate values.

 

Python Set

A Python set is an unordered collection of unique elements. It is usually denoted by a set of curly braces {}. Sets can be used to store many items, which could be in different data types, in a single variable. Python sets are mutable, which means they can be altered or modified. Below are examples of Python sets:

# integer set
int_set = {1,2,3,4,5,6,7,8}
print(int_set)
 
# {1, 2, 3, 4, 5, 6, 7, 8}
 
 
# string set
str_set = {"car", "bicycle", "skateboard", "scooter", "motorbike", "train"}
print(str_set)
 
# {'motorbike', 'train', 'scooter', 'bicycle', 'car', 'skateboard'}

We can also create a set object by using the set() function, like so:

# integer set
int_set = set((1,2,3,4,5,6,7,8))
print(int_set)
 
# {1, 2, 3, 4, 5, 6, 7, 8}
 
 
# string set
str_set = set(("car", "bicycle", "skateboard", "scooter", "motorbike", "train"))
print(str_set)
 
# {'motorbike', 'train', 'skateboard', 'scooter', 'car', 'bicycle'}

In each of the examples above, we parsed a tuple to the set() function to create the set.
 

Attributes of Python Set

Below are the attributes of the Python set:

  • A set is an unordered collection of unique elements. This means that the elements of a set have no inherent order.
  • Sets are mutable, which means that you can add, remove, or modify elements after the set has been created.
  • A set contains only unique elements. If you add an element to a set that is already in the set, the set will not change.
  • The elements of a set can be of different types, such as integers, strings, or even other sets.
  • You can iterate over the elements of a set using a for loop or other iterable functions.
  • Python sets to support a variety of set operations, such as union, intersection, and difference, which allow you to combine or compare sets in different ways.

Although lists and sets might have their respective unique attributes, they are very useful data structures in Python programming, and are often found in the foundations of robust solutions.

I, therefore, hope that this tutorial has been able to help you to understand the differences between lists and sets in Python.
 

Video, Further Resources & Summary

Do you need more explanations on the differences between lists and sets in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail the differences between lists and sets in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown the differences between lists and sets in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top