Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Find the Tuples Containing the Given Element from a List of Tuples - Python
Next article icon

The problem involves a list of elements, we need to count how many elements appear before the first occurrence of a tuple. If no tuple is found in the list, return the total number of elements in the list. To solve this, initialize a counter and use awhile loop to iterate through thelist, checking for atuplewithisinstance(). Return the count of elements before the first tuple or the length of the list if no tuple is found.

Using a for-loop withisinstance()

for loop iterates over each element in the list andisinstance() checks if the element is not a tuple. Thesum() function counts all non-tuple elements until the first tuple is encountered.

Python
a=[1,2,3,(4,5),6,7]c=0forelementina:ifisinstance(element,tuple):breakc+=1print(c)

Output
3

Explanation

  • Iterate through the list, check if the element is a tuple usingisinstance().
  • Stop counting when a tuple is encountered usingbreak.

Usingenumerate() andany()

Usingenumerate(), we iterate through the list with both the index and value.next() Function retrieves the index of the first tuple by checking each element withisinstance() and if no tuple is found, it defaults to the length of the list.

Python
a=[1,2,3,(4,5),6,7]c=next((ifori,xinenumerate(a)ifisinstance(x,tuple)),len(a))print(c)

Output
3

Explanation

  • Useenumerate() to get the index and element of the list.
  • Usenext() to find the index of the first tuple and return it; if no tuple is found, return the length of the list.

Using a while-loop

Using awhile loop, you can iterate through the list, checking each element withisinstance() until a tuple is encountered. The loop increments a counter and once a tuple is found, the iteration stops, providing the count of elements before the tuple.

Python
a=[1,2,3,(4,5),6,7]c=0whilec<len(a)andnotisinstance(a[c],tuple):c+=1print(c)

Output
3

Explanation

  • Use awhile loop to increment thecount until a tuple is found or the end of the list is reached.
  • Check if the current element is a tuple withisinstance() inside the loop.

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp