Untitled
unknown
plain_text
2 years ago
1.4 kB
8
Indexable
# List of tuples
list_of_tuples = [('apple', 10), ('banana', 12), ('cherry', 15)]
# Convert list of tuples to dictionary
dict_from_tuples = {key: value for key, value in list_of_tuples}
# Print the resulting dictionary
print(dict_from_tuples)
# Function to reverse a tuple
def reverse_tuple(t):
    return t[::-1]
# Function to swap two tuples
def swap_tuples(t1, t2):
    return t2, t1
# Test cases
tuple1 = ('apple', 10)
tuple2 = ('banana', 12)
print("Original tuples:")
print(tuple1)
print(tuple2)
reversed_tuple1 = reverse_tuple(tuple1)
swapped_tuples = swap_tuples(tuple1, tuple2)
print("\nReversed tuple1:")
print(reversed_tuple1)
print("\nSwapped tuples:")
print(swapped_tuples)
# Original tuple
tuple1 = ("Orange", [10, 20, 30], (5, 15, 25))
# Print the value 20
print("Value 20:")
for item in tuple1[1]:
    if item == 20:
        print(item)
# Modify the first item of the list inside the tuple
print("\nModified tuple:")
for i, item in enumerate(tuple1):
    if isinstance(item, list):
        item[0] = 220
        break
print(tuple1)
# Original tuple
tuple1 = (10, 20, 30, 20)
# Unpack the tuple into four variables
a, b, c, d = tuple1
# Display each variable
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)
# Count the occurrence of 20
count = tuple1.count(20)
print("\nOccurrence of 20:", count)
Editor is loading...
Leave a Comment