MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Python Tutorials: Difference between List & Array & Tuple & Set & Dict



ListArrayTuple
List is mutableArray is mutableTuple is immutable
A list is ordered collection of itemsAn array is ordered collection of itemsA tuple is an ordered collection of items
Item in the list can be changed or replacedItem in the array can be changed or replacedItem in the tuple cannot be changed or replaced
List can store more than one data typeArray can store only similar data typesTuple can store more than one data type

mutable

Mutable means that you can manipulate the list by adding to it, removing elements, updating already existing elements, etc. Sequence means that the elements are ordered, and indexed to start at index 0. The objects may be any object type in python, from other lists to functions to custom objects. Square brackets enclose the list values .

Mutable means that you can manipulate an array by adding or removing elements, updating already existing elements, etc. Sequence means that the elements are ordered, and indexed to start at index 0. However, unlike lists, arrays can only contains items of the same data type such as Ints, Floats, or Characters.

Immutable

Immutableย means that once a tuple is created, youย cannotย manipulate its contents, for example removing, updating or adding elements. Similar to lists above, the elements are in anย orderedย andย indexedย Sequence and can contain any pythonย Objects. However, the tuple is created usingย parentheses, which are just normal brackets().

odered

When we say that lists are ordered, it means that the items have a defined order, and that order will not change.

If you add new items to a list, the new items will be placed at the end of the list.

Unordered

Unordered means that they cannot be indexed. Key-value pairs means each element has a key which stores the value, and you can access an elementโ€™s value by its key.


Table comparing list, tuple and set on mutability, order, indexing and duplicates
Table comparing list, tuple, dictionary and set on duplicates, changeability, order and brackets
Table of Python list, tuple, set and dictionary with ordering, mutability, constructors and examples
List vs Set vs Dictionary vs Tuple chart with syntax, access, mutability, slicing and usage examples
Python tuples vs lists infographic comparing syntax, mutability, method counts and use as dict keys
Grid showing ordered, changeable, duplicate and indexed traits of list, tuple, dictionary and set

Summary cards: list general purpose, tuple immutable, set unique items, dict key/value pairs

Lists

A list is of an ordered collection data type that is mutable which means it can be easily modified and we can change its data values and a list can be indexed, sliced, and changed and each element can be accessed using its index value in the list. The following are the main characteristics of a List:

  • The list is an ordered collection of data types.
  • The list is mutable.
  • List are dynamic and can contain objects of different data types.
  • List elements can be accessed by index number.

# Python program to demonstrate List

list = ["mango", "strawberry", "orange",
		"apple", "banana"]
print(list)

# we can specify the range of the
# index by specifying where to start
# and where to end
print(list[2:4])

# we can also change the item in the
# list by using its index number
list[1] = "grapes"
print(list[1])
Code language: PHP (php)

array

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The following are the main characteristics of an Array:

  • An array is an ordered collection of the similar data types.
  • An array is mutable.
  • An array can be accessed by using its index number.

# Python program to demonstrate
# Creation of Array

# importing "array" for array creations
import array as arr

# creating an array with integer type
a = arr.array('i', [1, 2, 3])

# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
	print (a[i], end =" ")
print()

# creating an array with float type
b = arr.array('d', [2.5, 3.2, 3.3])

# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
	print (b[i], end =" ")
Code language: PHP (php)

tuple

A tuple is an ordered and an immutable data type which means we cannot change its values and tuples are written in round brackets. We can access tuple by referring to the index number inside the square brackets.  The following are the main characteristics of a Tuple:

  • Tuples are immutable and can store any type of data type.
  • it is defined using ().
  • it cannot be changed or replaced as it is an immutable data type.

tuple = ("orange","apple","banana")
print(tuple)

# we can access the items in
# the tuple by its index number
print(tuple[2])

#we can specify the range of the
# index by specifying where to start
# and where to end
print(tuple[0:2])
Code language: PHP (php)

Sets

We cannot talk about python data structures without mentioning Sets, which we will not get into much details in this article.

Sets are unordered collections of unique objects enclosed by curly braces{} โ€” not to be confused with dictionaries which are also enclosed with curly braces.

Unordered means the elements are not ordered therefore cannot be indexed. Unique means that a set cannot store duplicate values and are therefore very handy for removing duplicates from lists. You can store any python objects in a set. A set is created using Set={values}. You can convert a list into a set using Set(list).

Sets have their own unique operations for merging two sets. These are union() function or | operator, intersection() function or & operator, and the difference() function or โ€” operator.

Syntax Differences


list_num = [1,2,3,4]
tup_num = (1,2,3,4)

print(list_num)
print(tup_num)

type(list_num)
type(tup_num)Code language: PHP (php)

List of functions of โ€œlistโ€


Lists has more builtin function than that of tuple. We can use dir([object]) inbuilt function to get all the associated functions for list and tuple.


list_num = [1,2,3,4]
c=dir(list_num)
print(c)

Output:

['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']Code language: PHP (php)

List of functions of โ€œtupleโ€


tup_num = (1,2,3,4)
d=dir(tup_num)
print(d)

Output:

['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'count',
'index']Code language: PHP (php)

List of functions of โ€œarrayโ€

arrays = "123456"
a=dir(arrays)
print(a)

['__add__'
 '__class__'
 '__contains__'
 '__delattr__'
 '__dir__'
 '__doc__'
 '__eq__'
 '__format__'
 '__ge__'
 '__getattribute__'
 '__getitem__'
 '__getnewargs__'
 '__gt__'
 '__hash__'
 '__init__'
 '__init_subclass__'
 '__iter__'
 '__le__'
 '__len__'
 '__lt__'
 '__mod__'
 '__mul__'
 '__ne__'
 '__new__'
 '__reduce__'
 '__reduce_ex__'
 '__repr__'
 '__rmod__'
 '__rmul__'
 '__setattr__'
 '__sizeof__'
 '__str__'
 '__subclasshook__'
 'capitalize'
 'casefold'
 'center'
 'count'
 'encode'
 'endswith'
 'expandtabs'
 'find'
 'format'
 'format_map'
 'index'
 'isalnum'
 'isalpha'
 'isascii'
 'isdecimal'
 'isdigit'
 'isidentifier'
 'islower'
 'isnumeric'
 'isprintable'
 'isspace'
 'istitle'
 'isupper'
 'join'
 'ljust'
 'lower'
 'lstrip'
 'maketrans'
 'partition'
 'removeprefix'
 'removesuffix'
 'replace'
 'rfind'
 'rindex'
 'rjust'
 'rpartition'
 'rsplit'
 'rstrip'
 'split'
 'splitlines'
 'startswith'
 'strip'
 'swapcase'
 'title'
 'translate'
 'upper'
 'zfill']Code language: PHP (php)

List of functions of โ€œsetโ€


['__and__'
 '__class__'
 '__class_getitem__'
 '__contains__'
 '__delattr__'
 '__dir__'
 '__doc__'
 '__eq__'
 '__format__'
 '__ge__'
 '__getattribute__'
 '__gt__'
 '__hash__'
 '__iand__'
 '__init__'
 '__init_subclass__'
 '__ior__'
 '__isub__'
 '__iter__'
 '__ixor__'
 '__le__'
 '__len__'
 '__lt__'
 '__ne__'
 '__new__'
 '__or__'
 '__rand__'
 '__reduce__'
 '__reduce_ex__'
 '__repr__'
 '__ror__'
 '__rsub__'
 '__rxor__'
 '__setattr__'
 '__sizeof__'
 '__str__'
 '__sub__'
 '__subclasshook__'
 '__xor__'
 'add'
 'clear'
 'copy'
 'difference'
 'difference_update'
 'discard'
 'intersection'
 'intersection_update'
 'isdisjoint'
 'issubset'
 'issuperset'
 'pop'
 'remove'
 'symmetric_difference'
 'symmetric_difference_update'
 'union'
 'update']Code language: JavaScript (javascript)

List of functions of โ€œdictionariesโ€


['__class__'
 '__class_getitem__'
 '__contains__'
 '__delattr__'
 '__delitem__'
 '__dir__'
 '__doc__'
 '__eq__'
 '__format__'
 '__ge__'
 '__getattribute__'
 '__getitem__'
 '__gt__'
 '__hash__'
 '__init__'
 '__init_subclass__'
 '__ior__'
 '__iter__'
 '__le__'
 '__len__'
 '__lt__'
 '__ne__'
 '__new__'
 '__or__'
 '__reduce__'
 '__reduce_ex__'
 '__repr__'
 '__reversed__'
 '__ror__'
 '__setattr__'
 '__setitem__'
 '__sizeof__'
 '__str__'
 '__subclasshook__'
 'clear'
 'copy'
 'fromkeys'
 'get'
 'items'
 'keys'
 'pop'
 'popitem'
 'setdefault'
 'update'
 'values']Code language: JavaScript (javascript)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

What is Arrays and use cases of Arrays?

What is Arrays? An array is a data structure that stores a collection of elements of the same data type in a sequential manner. Each element in…

Read More

Python Tutorials: Data Types – List

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

List of Algorithms and Data Structures

Here’s a comprehensive list of Algorithms and Data Structures categorized for easy understanding: ๐Ÿ”น Data Structures Data structures help in organizing and storing data efficiently. 1. Linear…

Read More

How to Optimize Code Performance for High-Load Applications?

If you have ever encountered a certain application within a company, you would understand the frustration in terms of latency spikes, database bottlenecks, unpredictable slowdowns. These factors…

Read More

Why math is important if you want to become a programmer

In the realm of programming, mathematics serves as both a cornerstone and a catalyst for innovation. At its core, programming is a discipline rooted in logical problem-solving,…

Read More

Algorithms and Data Structures

Algorithms and data structures are two of the most important concepts in computer science. Algorithms are step-by-step procedures for solving problems, while data structures are ways of…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x