Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.3 kB
1
Indexable
Never
list_of_tuples = [('apple', 10), ('banana', 12), ('cherry', 15)]


dict_from_tuples = {key: value for key, value in list_of_tuples}


print(dict_from_tuples)



tup = (1, 2, 3, 4, 5)


reversed_tup = tup[::-1]


print(reversed_tup)



def swap_tuples(tup1, tup2):
    
    if len(tup1) != len(tup2):
        return "Error: Tuples must have the same length."
    
   
    swapped_tuples = [(tup2[i], tup1[i]) for i in range(len(tup1))]
    
   
    swapped_tup1, swapped_tup2 = zip(*swapped_tuples)
    
    return swapped_tup1, swapped_tup2


tup1 = (1, 2, 3)
tup2 = (4, 5, 6)

swapped_tup1, swapped_tup2 = swap_tuples(tup1, tup2)

print("Original tuples:", tup1, tup2)
print("Swapped tuples:", swapped_tup1, swapped_tup2)




tuple1 = ("Orange", [10, 20, 30], (5, 15, 25))


print("Value 20:")
for item in tuple1[1]:
    if item == 20:
        print(item)


print("\nModified tuple:")
for i, item in enumerate(tuple1):
    if isinstance(item, list):
        item[0] = 220
        break
print(tuple1)




tuple1 = (10, 20, 30, 20)


a, b, c, d = tuple1


print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)

count = tuple1.count(20)
print("\nOccurrence of 20:", count)
Leave a Comment