Untitled
unknown
plain_text
2 years ago
2.0 kB
7
Indexable
def square_list(input_list):
# Create a new list to store the squared values
output_list = []
# Iterate over each element in the input list
for i in input_list:
# Add the squared value of the element to the output list
output_list.append(i**2)
# Return the output list
return output_list
# Example usage
input_list = [1, 2, 3, 4, 5]
print(square_list(input_list)) # Output: [1, 4, 9, 16, 25]
def concatenate_lists(list1, list2):
# Create an empty list to store the concatenated strings
output_list = []
# Iterate over each element in the first list
for i in list1:
# Iterate over each element in the second list
for j in list2:
# Add the concatenated string to the output list
output_list.append(i + j)
# Return the output list
return output_list
# Example usage
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
print(concatenate_lists(list1, list2)) # Output: ['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']
def remove_empty_strings(input_list):
# Use the filter function to remove empty strings
output_list = list(filter(lambda x: x != '', input_list))
# Return the output list
return output_list
# Example usage
input_list = ["Mike", "", "Emma", "Kelly", "", "Brad"]
print(remove_empty_strings(input_list)) # Output: ['Mike', 'Emma', 'Kelly', 'Brad']
def display_lists(list1, list2):
# Combine the two lists into a list of tuples using the zip function
combined_list = list(zip(list1, list2[::-1]))
# Iterate over each tuple in the combined list
for item in combined_list:
# Print the first element of the tuple (from list1) followed by a space, and then the second element of the tuple (from list2)
print(item[0], item[1][::-1], sep='')
# Example usage
list1 = ["apple", "banana", "cherry"]
list2 = ["orange", "pear", "grape"]
display_lists(list1, list2)
# Output:
# apple egro
# banana reap
# cherry yrragEditor is loading...
Leave a Comment