
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Check if a number and its triple exists in an array
When it is required to check if a number and its triple exist in a list, a method is defined that iterates over the list, and sees if a number and the number multiplied by 3 is present.
Example
Below is a demonstration of the same
def check_triple_exists(my_list): for i in range(len(my_list)): for j in (my_list[:i]+my_list[i+1:]): if 3*my_list[i] == j: print("The triple exists") my_list = [67, 34, 89, 67, 90, 15, 5] print("The list is :") print(my_list) check_triple_exists(my_list)
Output
The list is : [67, 34, 89, 67, 90, 15, 5] The triple exists
Explanation
A method named ‘check_triple_exists’ is defined that takes a list as a parameter.
It iterates through the list, and multiple every element with 3 and checks to see if there exists a number that matches this doubled value.
If such a value is found, relevant message is displayed.
Outside the method, a list is defined, and is displayed on the console.
The method is called by passing the list.
The output is displayed on the console.
- Related Articles
- Python - Check if a number and its double exists in an array
- A number and its triple in Python
- Check if subarray with given product exists in an array in Python
- How to check if an item exists in a C# array?
- Check if a value exists in an array and get the next value JavaScript
- Check if a PHP cookie exists and if not set its value
- How do I check if a Python variable exists?
- How to check if a key exists in a Python dictionary?
- Check if an array is sorted and rotated in Python
- Check if table exists in MySQL and display the warning if it exists?
- Check if a word exists in a grid or not in Python
- Check if a File exists in C#
- Check if a number is an Achilles number or not in Python
- Check if a list exists in given list of lists in Python
- Check if a triplet with given sum exists in BST in Python

Advertisements