psc
Find the Smallest and the Largest List Elements on Inputs Provided by the User
l = []
num = int(input("How many numbers: "))
for n in range(num):
numbers = int(input("Enter number: "))
l.append(numbers)
print("Smallest element is", min(l))
print("Largest element is", max(l))
Split a List in Half and Store the Elements in Two Different Lists
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Print the first half of the list
print(l[:5])
# Print the second half of the list
print(l[5:])
Remove Multiple Empty Strings From a List of Strings
l = ["hello", "", "goodbye", "", "welcome", ""]
# Use list comprehension to filter out empty strings
l = [i for i in l if i != ""]
print(l)
Python program to interchange first and last element in a list
l = [1, 2, 3, 4, 5]
# Swap the first and last elements
l[0], l[-1] = l[-1], l[0]
print(l)
Print Elements With Frequency Greater Than a Given Value k
l = [1, 1, 1, 2, 3, 4, 5, 5, 3, 4, 7, 3, 2, 2, 6, 7, 8, 9, 10]
k = 2
# Use a set to avoid duplicate prints
printed_elements = set()
for i in l:
if l.count(i) > k and i not in printed_elements:
print(i)
printed_elements.add(i)
Find All Possible Combinations of a List With Three Elements
l = [1, 2, 3]
for i in l:
for j in l:
for k in l:
if i != j and j != k and i != k:
print(i, j, k)
Square Each Element of the List and Print List in Reverse Order
l = [1, 2, 3, 4]
# Print the square of each element
for i in l:
print(i ** 2)
# Reverse the list
l.reverse()
print(l)
Remove All Occurrences of an Element From a Given List
l = [1,2,3,4,5,6,7,8,9,10]
l.remove(5)
print(l)
Remove Empty List From a Given List.
l = [1, 2, [], 4, 5, [], 7, 8]
# Use list comprehension to filter out empty lists
l = [i for i in l if i != []]
print(l)
Python program to swap any two elements in the list
a = [1, 2, 3, 4, 5]
# Swap the elements at indices 2 and 4
a[2], a[4] = a[4], a[2]
print(a)
Python program to find the cumulative sum of elements of a list OR Python program to find the sum of number digits in list
l = [1, 2, 3, 4, 5]
sum = 0
# Calculate the sum
for i in l:
sum += i
# Print the final sum
print(sum)
How to clone or copy a list in Python? Cloning or copying a list
l = [1, 2, 3, 4, 5]
# Create a copy of the list l
l1 = l.copy()
# Print the copied list
print(l1)
Find the number of elements in a list in Python
l = [1, 2, 3, 4, 5]
# Print the length of the list
print(len(l))
Python program to get records with value at K index
def get_records_with_value_at_k_index(records, k, value):
return [record for record in records if len(record) > k and record[k-1] == value]
# Test data
records = [(5, 7), (4, 8, 3, 1), (5, 8, 0), (1, 2, 3, 4, 5, 7)]
ele = 8
k = 2
# Get records that have 'ele' at index 'k-1'
result = get_records_with_value_at_k_index(records, k, ele)
print(result)
Python program to check if the given tuple is a true record or not(Hint: use ALL() function)
def is_true_record(tpl):
if len(tpl) > 0:
return True
else:
return False
# Test tuples
tuple1 = (1, 2, 3, 4, 5)
tuple2 = ()
# Print results
print(is_true_record(tuple1)) # Expected: True
print(is_true_record(tuple2)) # Expected: False
Python program to concatenate consecutive elements in tuple using zip() function:
def concatenate_elements(test_tup):
result = []
for element in zip(*test_tup):
result.append(''.join(element))
return result
# Test tuple
test_tup = ('a', 'b', 'c', 'd')
# Get the result
result = concatenate_elements(test_tup)
# Print the result
print(result)
Python program to perform AND operation on tuples using map() function.
tup1 = (3, 1, 4)
tup2 = (5, 2, 6)
# Printing original tuples
print("The original tuple 1:", tup1)
print("The original tuple 2:", tup2)
# Using zip and bitwise & to combine tuples
c = tuple(v1 & v2 for v1, v2 in zip(tup1, tup2))
# Print the result
print("Resulting tuple:", c)
Python program to sort list of tuples alphabetically at index 1
tup1 = [('b', 23), ('c', 37), ('a', 11), ('d', 29)]
# Sort the list of tuples by the second element (index 1) alphabetically
tup1.sort(key=lambda x: x[1])
# Print the sorted list
print(tup1)
Python program to find the index of minimum value record from list of tuples at 0th index
tup1 = [('b', 23), ('c', 37), ('a', 11), ('d', 29)]
# Find the index of the tuple with the minimum value at the 0th index
min_index = min(range(len(tup1)), key=lambda i: tup1[i][0])
# Print the result
print("Index of minimum value record at 0th index:", min_index)
Python program to perform pairwise addition in tuples
mytuple = (3, 1, 7, 9, 2)
# Print the original tuple
print("The elements are: " + str(mytuple))
# Perform pairwise addition of elements in the tuple
additiontuple = tuple(x + y for x, y in zip(mytuple, mytuple[1:]))
# Print the result of pairwise addition
print("The pairwise addition is: " + str(additiontuple))
Python program to extract maximum value in record given as list of tuples at index 2 as tuple attribute
list = [('a', 23), ('b', 37), ('c', 11), ('d', 29)]
# Find the tuple with the maximum value based on the second element (index 1)
max_value = max(list, key=lambda x: x[1])
# Print the result
print(max_value)
Python program to find modulo of tuple elements
tuple1 = (13, 18, 17, 19, 12)
tuple2 = (4, 2, 3, 7, 2)
# Perform element-wise modulo operation
result = tuple(x % y for x, y in zip(tuple1, tuple2))
# Print the result
print(result)
Python program to raise elements of tuple as a power to another tuple
tup1 = (1, 2, 3, 4)
tup2 = (1, 2, 3, 4)
# Perform element-wise exponentiation (x ** y)
result = tuple(x ** y for x, y in zip(tup1, tup2))
# Print the result
print(result)
Python program to create a tuple from string and list
my_string = "Hello"
my_list = [1, 2, 3]
# Convert both string and list to tuples and store them in a tuple
my_tuple = (tuple(my_string), tuple(my_list))
# Print the resulting tuple of tuples
print(my_tuple)
Python program to convert tuple to integer
my_tuple = (1, 2, 3)
# Iterate over each element in the tuple and convert it to an integer (though it's already an integer)
for i in my_tuple:
integer = int(i) # Convert to int (this step is redundant in this case)
print(integer)
Python program to check if the element is present in tuple
tup = (1, 2, 3, 4, 5)
ele = 3
# Check if the element is in the tuple
if ele in tup:
print("Element is present in tuple")
else:
print("Element is not present in tuple")
Python program to check if a tuple is a subset of another tuple
tup1 = (1, 2, 3, 4, 5)
tup2 = (3, 4, 5)
# Check if all elements of tup2 are in tup1
if all(ele in tup1 for ele in tup2):
print("Tuple is a subset of another tuple")
else:
print("Tuple is not a subset of another tuple")
Python program to convert tuple to adjacent pair dictionary
my_tuple = (1, 2, 3, 4, 5, 6)
my_dict = {}
# Iterate through the tuple in steps of 2
for i in range(0, len(my_tuple), 2):
# Check if the next element exists
if i + 1 < len(my_tuple):
my_dict[my_tuple[i]] = my_tuple[i + 1]
else:
my_dict[my_tuple[i]] = None
# Print the resulting dictionary
print(my_dict)
Python program to convert tuple to float value
my_tuple = (1, 2, 3, 4, 5)
# Convert each element to a float and print
for i in my_tuple:
float_value = float(i)
print(float_value)
Python program to concatenate tuples to nested tuple
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
# Concatenate tuple1 and tuple2
nested_tuple = tuple1 + tuple2
# Print the resulting tuple
print(nested_tuple)
Python program to perform summation of tuple in list
tuple1 = (1, 2, 3, 4, 5)
sum=0
for i in tuple1: sum+=i print(sum)