Difference Between List and Set

Difference Between List and Set

5 mins read1.2K Views Comment
Vikram Singh
Assistant Manager - Content
Updated on May 15, 2023 11:46 IST

In this article, we will briefly cover what lists and sets are in Java and Python, the difference between them, and the features of lists and sets.


The key difference between a list and a set is that list can store duplicate values, whereas a set can’t. In Python, lists and sets are built-in data structures that store and arrange the values. While in Java, lists and sets are the interfaces of the collection framework. This article will briefly discuss what lists and sets are in Java and Python and their differences. 

Let’s start the article with the difference between List and Set based on different parameters.

Table of Content

What are the Differences Between a List and a Set?

Parameter List Set
Indexing It supports indexing, i.e., a list is an indexed sequence. It doesn’t support indexing, i.e., the set is a non-indexed sequence.
Order It maintains the order of the element. It doesn’t maintain the order of the element.
Duplicates Duplicate values are allowed in the list. Set always contains the unique value, i.e., It does not contain any duplicate value. 
Null Elements Multiple null values can be stored. Only one Null value can be stored.
Positional Access Yes No
Mutable Yes, list elements can be modified. Yes, sets element can be modified.
Use Cases Suitable for Sequence, ordered data Suitable for storing unordered data

What is List in JAVA?

A list in Java is a commonly used data structure representing an ordered collection of elements. It is a part of the Java Collection Framework. Java offers multiple implementations of the list interface, each with its own characteristics, such as Array List and LinkedList.

  • An array backs ArrayList and provides fast access to elements by their indices.
  • LinkedList uses a doubly linked list structure and is useful for frequent insertion and deletion at both ends of the list.

Let’s take an example to get a better understanding of lists in Java.

import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
// Create a list to store numbers
List<Integer> numberList = new ArrayList<>();
// Add elements to the list
// Access and print elements in the list
System.out.println("Elements in the number list:");
for (int number : numberList) {
// Get the size of the list
int listSize = numberList.size();
System.out.println("Size of the number list: " + listSize);
// Update an element in the list
numberList.set(1, 25);
// Remove an element from the list
// Check if the list contains a specific element
boolean containsThirty = numberList.contains(30);
System.out.println("Does the number list contain 30? " + containsThirty);
Copy code



What is List in Python?

The Python list object is the most general mutable sequence type provided by the languages. Python List is mutable that is the contents of the list be modified in place. You can change a list in place, add new elements, overwrite existing elements or delete them as well. Also, note that in a list, the same value can occur more than once.

Let’s take an example to get a better understanding of List in Python.

# Creating a list of integers
number_list = [1, 2, 3, 4, 5]
# Adding elements to the list
number_list.insert(0, 0)
# Modifying an element in the list
number_list[3] = 100
# Removing an element from the list
# Accessing elements in the list
print("Elements in the number list:")
for number in number_list:
# Checking the length of the list
list_length = len(number_list)
print("Length of the number list:", list_length)
Copy code


All About Python Lists Methods
All About Python Lists Methods
In this guide, you will learn all about lists in Python: accessing lists, changing list elements, looping through a list, and other list operations with examples.
Python Lists Practice Programs For Beginners
Python Lists Practice Programs For Beginners
Test your Python Lists knowledge with practice programs! In this blog, we will solve 12 python lists exercises for beginners.

Features of List

  • It allows duplicate elements.
    • i.e., it can have multiple occurrences of the same element in a list.
  • Elements in the list maintain a specific order.
    • This order can be modified by adding, removing, or rearranging elements.
  • It can store different data types within the same list, such as integer, string, and object.
  • Elements of the list can be accessed using the indexes.
    • The list also supports slicing to retrieve multiple elements at once.

Example of List Usage

  • Creating a to-do list where the order of tasks matters.
  • Storing a collection of student names in a class roster.
  • Tracking the history of visited web pages in a web browser.

Must Check: What is Java?

Must Check: Top Java Online Courses and Certifications

What is Set in Java?

A set in Java is a commonly used data structure representing an unordered collection of unique elements. Similar to the list in Java, it is also part of the collection framework that provides a set of classes and interfaces to handle various types of collections.

Let’s take an example to understand Sets in Java better.

import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
// Create a set of integers
Set<Integer> numberSet = new HashSet<>();
// Add elements to the set
numberSet.add(10); // Adding a duplicate element
// Print the set
System.out.println("Number Set: " + numberSet);
// Size of the set
System.out.println("Size of the set: " + numberSet.size());
Copy code




In the above example:

A set is created (numberSet) to store integers, and HashSet implementation is used to provide efficient lookup and insertion operations. Four elements (5, 10, 15, and 10) are inserted into the set. And when we print the set, the duplicate element (10) is removed, and you will get only three integers (5, 10, and 10).

Now, apart from this, you can use:

  • contains() – to check if an element exists in the set.
  • remove() – to remove elements.
  • isEmpty() – to check if the set is empty.

What is Set in Python?

Python sets are unordered collections of unique elements and immutable objects. You can think of them as a dictionary with just the keys and its values thrown away. Therefore, each item of a set should be unique.

Let’s take an example to get a better understanding of set in Python.

#difference between two lists- Using set()
def diff_between(list1, list2):
A = set(list1)
B = set(list2)
#return list(A - B) + list(B - A)
return list({*A.difference(B),*B.difference(A)})
# Driver Code
List1 = [10, 45, 34, 20, 11]
List2 = [11, 25, 45, 20]
print(diff_between(List1, List2))
Copy code


Python Sets Practice Programs For Beginners
Python Sets Practice Programs For Beginners
Python offers a rich collection of sequence object types, and sets are one of them.
Understanding Python Sets (With Examples)
Understanding Python Sets (With Examples)
In this article, you will discuss about Python Sets: creating sets, accessing, adding, and removing set items, looping through a set, and other set operations with the help of examples.

Features of Set

  • It doesn’t maintain any element order.
  • Similar to the list, sets are also mutable.
    • i.e., you can add, edit or delete the element.
  • The set doesn’t contain duplicate elements.
    • It automatically removes the duplicate and ensures only unique entries appear.
  • It allows multiple mathematical operations such as union, intersection, and difference.
  • Set provides efficient membership testing that quickly checks whether the element exists.

Example of Set Usage

  • Keeping a collection of unique email addresses in an email subscription list.
  • Storing a set of unique usernames in a user database.
  • Finding common elements between two sets, such as common friends in social networks.

Must Check: What is Python?

Must Check: Top Python Online Courses and Certifications


In this article, we have briefly covered what lists and sets are in Java and Python, the difference between them, and the features of lists and sets.

Hope you will like the article.

Keep Learning!!

About the Author
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio