List | Array | Tuple |
---|---|---|
List is mutable | Array is mutable | Tuple is immutable |
A list is ordered collection of items | An array is ordered collection of items | A tuple is an ordered collection of items |
Item in the list can be changed or replaced | Item in the array can be changed or replaced | Item in the tuple cannot be changed or replaced |
List can store more than one data type | Array can store only similar data types | Tuple 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.
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])
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 =" ")
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])
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)
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']
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']
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']
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']
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']
- Top 10 Website Development Companies in Vadodara - December 20, 2024
- Compare SAST, DAST and RASP & its Tools for DevSecOps - December 19, 2024
- Comparing AWS, Azure, and Google Cloud in terms of services - December 19, 2024