5️⃣

17.5 Count and Index

Count and Index

Count tells you how many of a given element is in the tuple.
tup = tuple('mississippi') # each element of this tuple is a letter print(tup.count('i')) # prints 4
Index returns the index of the first occurrence of a given element in the tuple.
tup = tuple('mississippi') # each element of this tuple is a letter print(tup.index('s')) # prints 2
View on GitHub

Practice

Magic Tuple Number

A magic tuple number for a given element is the index of the given element’s first occurrence in the tuple TIMES the number of occurrences of the given element in the tuple. Given an element and a tuple, return the magic tuple number. If the element does not exist in the tuple, return -1.
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.