Input a list/tuple of elements, search for a given element in the list/tuple.

Input a list/tuple of elements and search for a given element in the list/tuple Python Program

# Python Program to input a list of 5 elements and search element in List

mylist = [] print("Enter 5 elements for the list: ") for i in range(5): value = int(input()) mylist.append(value) print("Enter an element to be search: ") element = int(input()) for i in range(5): if element == mylist[i]: print("\nElement found at Index:", i) print("Element found at Position:", i+1)

Output:

Enter 5 elements for the list: 45 34 37 34 55 Enter an element to be search: 34 ('\nElement found at Index:', 1) ('Element found at Position:', 2) ('\nElement found at Index:', 3) ('Element found at Position:', 4) >>>

You may also Check :

  • Find the largest/smallest number in a list/tuple

  • Input a list of numbers and swap elements at the even location with the elements at the odd location.

  • Input a list of numbers and find the smallest and largest number from the list.

  • Create a dictionary with the roll number, name and marks of n students in a class and display the names of students who have scored marks above 75.

Python | Find the tuples containing the given element from a list of tuples

Given a list of tuples, the task is to find all those tuples containing the given element, say n.
Examples:

Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)]
Output: [(11, 22), (11, 44)]
Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)]
Output: [(14, 3), (1, 3), (3, 3)]

There are multiple ways we can find the tuples containing the given element from a list of tuples. Let’s see some of Pythonic ways to do this task.
Method #2: Using list comprehension. It works only when there are fixed number of elements in every list. For example 2 elements in below code.




# Python code to find the tuples containing
# the given element from a list of tuples
# List of tuples
Input = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)]
# Find an element in list of tuples.
Output = [item for item in Input
if item[0] == 3 or item[1] == 3]
# printing output
print(Output)
Output: [(14, 3), (1, 3), (3, 3)]


Method #1 : Using filter In this solution, there can be variable number of nodes in lists.




# Python code to find the tuples containing
# the given element from a list of tuples
# List of tuples
Input = [(11, 22), (33, 55), (55, 77),
(11, 44), (33, 22, 100, 11), (99, 11)]
# Using filter
Output = list(filter(lambda x:11 in x, Input))
# Printing output
print(Output)
Output: [(11, 22), (11, 44), (33, 22, 100, 11), (99, 11)]


Input a list/tuple of elements, search for a given element in the list/tuple.




Article Tags :
Python
Python tuple-programs
python-tuple

Code:

# Python code to find the tuples containing # the given element from a list of tuples # List of tuples Input = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] # Find an element in list of tuples. Output = [item for item in Input if item[0] == 3 or item[1] == 3] # printing output print(Output)

Explanation:

  • Here first a list of tuple is entered with list as elements.
  • Then the condition is checked whether the first or second one of list is equal to 3.
  • If it is equal to 3 then it is presented as output in tuple containing list.

Output:

Input a list/tuple of elements, search for a given element in the list/tuple.
Input a list/tuple of elements, search for a given element in the list/tuple.
[(14, 3), (1, 3), (3, 3)]

Thank you for learning from this post. We wish all of the best for class 11 computer science with python exams. Python is one of the most easiest and powerful language. I am impressed that CBSE is teaching Python. I feel if you go into information and communication field your carrier will be secured. Python is mainly used in data analysis and artificial intelligence programs and software’s. With the whole new generation of students in CBSE board learning Python it gives me pride to devote time for solving and presenting the work. This practical file questions are solved by Experts and every day Programmers. Here we have presented the code in most simple way of programming and yet the most powerful code for our practical. For securing full marks bookmark this website as it will help you to sail through Practicals, projects and pythons questions and answers of CBSE computer science (Python.. I am sure computer science work is nice and benefit the learners. Here below whole practical Practical file is given with link.

Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study

Related

Find the tuples containing the given element from a list of tuples in Python

PythonServer Side ProgrammingProgramming

A list can have tuples as its elements. In this article we will learn how to identify those tuples which contain a specific search element which is a string.