Difference between list and tuple and dictionary and set in Python (2023)

Differences and Applications of List, Tuple, Set and Dictionary in Python

Lists: are just like dynamic sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python.

Show

  • Differences and Applications of List, Tuple, Set and Dictionary in Python
  • Applications of List, Set, Tuple, and Dictionary
  • Difference between List VS Set VS Tuple in Python
  • Table of Difference between List, Set, and Tuple
  • List Vs. Tuple Vs. Set Vs. Dictionary: Know the Difference Between List, Tuple, Set, and Dictionary in Python
  • Difference Between List, Tuple Set and Dictionary in Python
  • Tuples vs. Lists vs. Sets in Python
  • Video liên quan

Tuple: A Tuple is a collection of Python objects separated by commas. In some ways, a tuple is similar to a list in terms of indexing, nested objects, and repetition but a tuple is immutable, unlike lists that are mutable.

Set: A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Python’s set class represents the mathematical notion of a set.

Dictionary: in Python is an ordered (since Py 3.7) [unordered (Py 3.6 & prior)] collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized.

List, Tuple, Set, and Dictionary are the data structures in python that are used to store and organize the data in an efficient manner.



ListTupleSetDictionary
List is a non-homogeneous data structure that stores the elements in single row and multiple rows and columnsTuple is also a non-homogeneous data structure that stores single row and multiple rows and columnsSet data structure is also non-homogeneous data structure but stores in single rowDictionary is also a non-homogeneous data structure which stores key value pairs
List can be represented by [ ]

Tuple can be represented by

( )

Set can be represented by { }Dictionary can be represented by { }
List allows duplicate elementsTuple allows duplicate elementsSet will not allow duplicate elementsSet will not allow duplicate elements and dictionary doesn’t allow duplicate keys.
List can use nested among allTuple can use nested among allSet can use nested among allDictionary can use nested among all
Example: [1, 2, 3, 4, 5]Example: (1, 2, 3, 4, 5)Example: {1, 2, 3, 4, 5}Example: {1, 2, 3, 4, 5}
List can be created using list() functionTuple can be created using tuple() function.Set can be created using set() functionDictionary can be created using dict() function.
List is mutable i.e we can make any changes in list.Tuple is immutable i.e we can not make any changes in tupleSet is mutable i.e we can make any changes in set. But elements are not duplicated.Dictionary is mutable. But Keys are not duplicated.
List is orderedTuple is orderedSet is unorderedDictionary is ordered (Python 3.7 and above)

Creating an empty list

l=[]

Creating an empty Tuple

t=()

Creating a set

a=set()

b=set(a)

Creating an empty dictionary

d={}

Below is the program for implementation of List, tuple, set, and dictionary:

Python3

# Python3 program for explaining

# use of list, tuple, set and

# dictionary

# Lists

l = []

# Adding Element into list

l.append(5)

l.append(10)

print("Adding 5 and 10 in list", l)

# Popping Elements from list

l.pop()

print("Popped one element from list", l)

print()

# Set

s = set()

# Adding element into set

s.add(5)

s.add(10)

print("Adding 5 and 10 in set", s)

# Removing element from set

s.remove(5)

print("Removing 5 from set", s)

print()

# Tuple

t = tuple(l)

# Tuples are immutable

print("Tuple", t)

print()

# Dictionary

d = {}

# Adding the key value pair

d[5] = "Five"

d[10] = "Ten"

print("Dictionary", d)

# Removing key-value pair

del d[10]

print("Dictionary", d)

OutputAdding 5 and 10 in list [5, 10]Popped one element from list [5]Adding 5 and 10 in set {10, 5}Removing 5 from set {10}Tuple (5,)Dictionary {5: 'Five', 10: 'Ten'}Dictionary {5: 'Five'}

Applications of List, Set, Tuple, and Dictionary

List:

  • Used in JSON format
  • Useful for Array operations
  • Used in Databases

Tuple:

  • Used to insert records in the database through SQL query at a time.Ex: (1.’sravan’, 34).(2.’geek’, 35)
  • Used in parentheses checker

Set:

  • Finding unique elements
  • Join operations

Dictionary:

  • Used to create a data frame with lists
  • Used in JSON

Difference between list and tuple and dictionary and set in Python (1)


Article Tags :

Articles

Data Structures

Difference Between

Python

Python Programs

Python dictionary-programs

python-list

python-set

python-tuple

Practice Tags :

Data Structures

python-list

python-set

Read Full Article

Difference between List VS Set VS Tuple in Python

List: Lists are just like dynamic sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python. The main characteristics of lists are –

  • The list is a datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.
  • List are mutable .i.e it can be converted into another data type and can store any data element in it.
  • List can store any type of element.

Example:

Python3

# Python3 program to demonstrate

# List

# Creating a List

List = []

print("Blank List: ")

print(List)

# Creating a List of numbers

List = [10, 20, 14]

print("\nList of numbers: ")

print(List)

# Creating a List of strings and accessing

# using index

List = ["Geeks", "For", "Geeks"]

print("\nList Items: ")

print(List[0])

print(List[2])

Output:

Blank List:[]List of numbers:[10, 20, 14]List Items:GeeksGeeks

Tuple: Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. The main characteristics of tuples are –

  • Tuple is an immutable sequence in python.
  • It cannot be changed or replaced since it is immutable.
  • It is defined under parenthesis().
  • Tuples can store any type of element.

Example:



Python3

# Creating an empty Tuple

Tuple1 = ()

print("Initial empty Tuple: ")

print (Tuple1)

# Creating a Tuple with

# the use of list

list1 = [1, 2, 4, 5, 6]

print("\nTuple using List: ")

print(tuple(list1))

#Creating a Tuple

#with the use of built-in function

Tuple1 = tuple('Geeks')

print("\nTuple with the use of function: ")

print(Tuple1)

Output:

Initial empty Tuple:()Tuple using List:(1, 2, 4, 5, 6)Tuple with the use of function:('G', 'e', 'e', 'k', 's')

Set: In Python, Set is an unordered collection of data type that is iterable, mutable, and has no duplicate elements. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. The main characteristics of set are –

  • Sets are an unordered collection of elements or unintended collection of items In python.
  • Here the order in which the elements are added into the set is not fixed, it can change frequently.
  • It is defined under curly braces{}
  • Sets are mutable, however, only immutable objects can be stored in it.

Example:

Python3

# Python3 program to demonstrate

# Set in Python

# Creating a Set

set1 = set()

print("Initial blank Set: ")

print(set1)

# Creating a Set with

# the use of Constructor

# (Using object to Store String)

String = 'GeeksForGeeks'

set1 = set(String)

print("\nSet with the use of an Object: " )

print(set1)

# Creating a Set with

# the use of a List

set1 = set(["Geeks", "For", "Geeks"])

print("\nSet with the use of List: ")

print(set1)

Output:

Initial blank Set:set()Set with the use of an Object:{'G', 's', 'e', 'o', 'r', 'F', 'k'}Set with the use of List:{'Geeks', 'For'}

Table of Difference between List, Set, and Tuple

ListSetTuple
Lists is MutableSet is MutableTuple is Immutable
It is Ordered collection of itemsIt is Unordered collection of itemsIt is Ordered collection of items
Items in list can be replaced or changedItems in set cannot be changed or replacedItems in tuple cannot be changed or replaced

Difference between list and tuple and dictionary and set in Python (2)


Article Tags :

Difference Between

Python

python-list

python-set

python-tuple

Practice Tags :

python-list

python-set

Read Full Article

List Vs. Tuple Vs. Set Vs. Dictionary: Know the Difference Between List, Tuple, Set, and Dictionary in Python

All of these are elements used in the Python language, but there is a fundamental difference between list, tuple, set, and dictionary in Python. We will discuss the same in detail ahead, but let us first know a bit more about each of these.

Difference Between List, Tuple Set and Dictionary in Python

Learn about difference between list, tuple set and dictionary in Python.

R

Rajpreet Singh Nayyar

1 Dec 2021-9 mins read

Tuples vs. Lists vs. Sets in Python

  • Difference between list and tuple and dictionary and set in Python (3)

Jerry Ng

Aug 10, 2021 4 min read

Photo by Tom Wilson / Unsplash

In Python, there are four built-in data types that we can use to store collections of data. With different qualities and characteristics, these built-in data types are List (list), Tuple (tuple), Set (set), and Dictionary (dict).

In this article, we are going to dig into the rabbit holes of List, Tuple, and Set in Python. We will go through their differences and when to use these data types.

As Dictionary associates keys with their respective values, which is a very different use case compared to List, Tuple, and Set (which simply just contain values), it won’t be part of this discussion.

For the sake of simplicity, I will use Set and Dictionary interchangeably, as they are based on Hash Table (or Hash Map).

Difference between list and tuple and dictionary and set in Python (4)

Python built-in data types to store collections of data
Top Articles
Latest Posts
Article information

Author: Stevie Stamm

Last Updated: 10/14/2022

Views: 6277

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.