xxxxxxxxxx
#It's like a list, but unchangeable
tup = ("var1","var2","var3")
tup = (1,2,3)
#Error
xxxxxxxxxx
"""tuples in python:
the lists are mutable, Python needs to allocate an extra memory block
in case there is a need to extend the size of the list object after it is created.
In contrary, as tuples are immutable and fixed size, Python allocates
just the minimum memory block required for the data.
As a result, tuples are more memory efficient than the lists."""
import sys, platform, time
a_list = list()
a_tuple = tuple()
a_list = [1,2,3,4,5]
a_tuple = (1,2,3,4,5)
print(sys.getsizeof(a_list))
print(sys.getsizeof(a_tuple))
start_time = time.time()
b_list = list(range(10000000))
end_time = time.time()
print("Instantiation time for LIST:", end_time - start_time)
start_time = time.time()
b_tuple = tuple(range(10000000))
end_time = time.time()
print("Instantiation time for TUPLE:", end_time - start_time)
start_time = time.time()
for item in b_list:
aa = b_list[20000]
end_time = time.time()
print("Lookup time for LIST: ", end_time - start_time)
start_time = time.time()
for item in b_tuple:
aa = b_tuple[20000]
end_time = time.time()
print("Lookup time for TUPLE: ", end_time - start_time)
"""As you can see from the output of the above code snippet,
the memory required for the identical list and tuple objects is different.
When it comes to the time efficiency, again tuples have a slight advantage
over the lists especially when lookup to a value is considered."""
"""When to use Tuples over Lists:
Well, obviously this depends on your needs.
There may be some occasions you specifically do not what data to be changed.
If you have data which is not meant to be changed in the first place,
you should choose tuple data type over lists.
But if you know that the data will grow and shrink during the runtime of the application,
you need to go with the list data type."""
xxxxxxxxxx
#a tuple is basically the same thing as a
#list, except that it can not be modified.
tup = ('a','b','c')
xxxxxxxxxx
from collections import namedtuple
Point = namedtuple('Point', 'x y')
p = Point(1, y=2)
p[0]
# 1
p.x
# 1
getattr(p, 'y')
# 2
p._fields # Or: Point._fields
# ('x', 'y')
xxxxxxxxxx
# Creating a tuple using ()
t = (1, 2, 4, 5, 4, 1, 2,1 ,1)
print(t.count(1))
print(t.index(5))
xxxxxxxxxx
# tuple = immutable, cannot be modified, efficient memory usage
# Best practice : Add a comma at the end
some_tuple = (1,2,3,)
# (23) is not a tuple, rather a mathematical bracket operation
# (23,) is a tuple
# you can check it with print(type(your_tuple_variable))
tuple_with_single_value = (100,)
xxxxxxxxxx
# tuples are immutable
# initialize tuple
scores = (10, 20, 30)
# check if 10 is in scores tutple
print(10 in scores)
# print all scores in tuple
for score in scores:
print(scores)
# print length of tuple
print(len(scores))
# print scores reversed
print(tuple(reversed(scores)))
# print scores reversed
print(scores[::-1])
# print first score by index
print(scores[0])
# print first two scores
print(scores[0:2])
# print tuple
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
xxxxxxxxxx
# Creating a Tuple with
# the use of Strings
Tuple = ('Geeks', 'For')
print("\nTuple with the use of String: ")
print(Tuple)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
Tuple = tuple(list1)
# Accessing element using indexing
print("First element of tuple")
print(Tuple[0])
# Accessing element from last
# negative indexing
print("\nLast element of tuple")
print(Tuple[-1])
print("\nThird last element of tuple")
print(Tuple[-3])
xxxxxxxxxx
# Different types of tuples
# Empty tuple
my_tuple = ()
print(my_tuple) # ()
# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple) # (1, 2, 3)
# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple) # (1, 'Hello', 3.4)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple) # ('mouse', [8, 4, 6], (1, 2, 3))
xxxxxxxxxx
# Create a tuple
my_tuple = (1, 2, 3, 4, 5)
# Accessing elements in a tuple
print(my_tuple[0]) # Output: 1
print(my_tuple[2:4]) # Output: (3, 4)
# Find the index of a specific element in a tuple
print(my_tuple.index(3)) # Output: 2
# Get the count of a specific element in a tuple
print(my_tuple.count(4)) # Output: 1
# Convert a tuple to a list
my_list = list(my_tuple)
print(my_list) # Output: [1, 2, 3, 4, 5]
# Convert a list to a tuple
new_tuple = tuple(my_list)
print(new_tuple) # Output: (1, 2, 3, 4, 5)
# Check if an element exists in a tuple
print(4 in my_tuple) # Output: True
# Get the length of a tuple
print(len(my_tuple)) # Output: 5
# Concatenate two tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
new_tuple = tuple1 + tuple2
print(new_tuple) # Output: (1, 2, 3, 4, 5, 6)