Python Practice › Tuple Programs
Python Tuple Programs with Solutions
70 tuple programs: creating, indexing, unpacking, sorting lists of tuples, converting between tuples, lists, strings and dicts, and filtering tuples by their contents. Each solution is explained and shows its real output.
Before you start
Read the Tuples and List vs Tuple lesson first if the topic is new to you.
All programs
- #1Tuple of pairs from two lists
Write a Python program to create a tuple of pairs from two lists.Easy - #2Maximum value in a tuple
Write a Python program to find the maximum value in a tuple.Easy - #3Minimum value in a tuple
Write a Python program to find the minimum value in a tuple.Easy - #4List of (number, square) tuples
Write a Python program to create a list of tuples, each holding a number and its square.Easy - #5Tuple with different data types
Write a Python program to create a tuple containing values of different data types.Easy - #6Element by index
Write a Python program to get an element of a tuple by its index.Easy - #7Unpack a tuple into variables
Write a Python program to assign the values of a tuple to several variables and print them.Easy - #8Add an item to a tuple
Write a Python program to add an item to a tuple.Easy - #9Tuple of characters to a string
Write a Python program to join a tuple of characters into a string.Easy - #102nd from the front, 3rd from the back
Write a Python program to get the 2nd element from the front and the 3rd element from the back of a tuple.Easy - #11Is an element in a tuple?
Write a Python program to check whether an element exists in a tuple.Easy - #12Add a list's items to a tuple
Write a Python program to add the items of a list to a tuple.Easy - #13Sum of a tuple
Write a Python program to find the sum of the elements of a tuple.Easy - #14Add values row by row to a tuple matrix
Write a Python program to add one value from a tuple to each row of a tuple matrix.Medium - #15Tuple of squares from a list
Write a Python program to create a tuple of the squares of a list’s elements.Easy - #16Multiply adjacent elements
Write a Python program to multiply each pair of neighbouring elements of a tuple.Easy - #17Join tuples with the same first element
Write a Python program to join tuples that start with the same element.Medium - #18List to tuple, doubled
Write a Python program to convert a list into a tuple with each element multiplied by 2.Easy - #19Remove an item from a tuple
Write a Python program to remove an item from a tuple.Easy - #20Slice a tuple
Write a Python program to slice a tuple.Easy - #21Index of an element
Write a Python program to find the index of an element in a tuple.Easy - #22Length of a tuple
Write a Python program to find the length of a tuple.Easy - #23Tuple of pairs to a dictionary
Write a Python program to convert a tuple of (value, key) pairs into a dictionary keyed by the second item.Easy - #24Reverse a tuple
Write a Python program to reverse a tuple.Easy - #25Group a list of pairs into a dictionary
Write a Python program to group (key, value) tuples into a dictionary of lists.Easy - #26Remove tuples of a given length
Write a Python program to remove tuples of length i from a list.Easy - #27Remove tuples containing None
Write a Python program to remove tuples that contain None.Easy - #28Remove tuples that are all None
Write a Python program to remove tuples in which every element is None.Easy - #29Sort tuples by the first item
Write a Python program to sort a list of tuples by their first item.Easy - #30Sort tuples by the second item
Write a Python program to sort a list of tuples by their second item.Easy - #31Sort tuples by length
Write a Python program to sort a list of tuples by length.Easy - #32Frequency of tuple elements
Write a Python program to count how often each element appears in a tuple.Easy - #33Tuples with more than 3 elements
Write a Python program to keep tuples with more than 3 elements.Easy - #34How often each tuple appears
Write a Python program to count how many times each tuple appears in a list.Easy - #35Tuple at a given position
Write a Python program to get the tuple at index i of a list of tuples.Easy - #36Are all tuple elements distinct?
Write a Python program to test whether all elements of a tuple are distinct.Easy - #37Tuple to a string
Write a Python program to convert a tuple to a string.Easy - #38Remove empty tuples
Write a Python program to remove empty tuples (and tuples holding only an empty string) from a list.Easy - #39Sort tuples by a float element
Write a Python program to sort tuples by their float element.Easy - #40Count elements before a tuple
Write a Python program to count the elements of a list until the first tuple is reached.Easy - #41Product of a tuple
Write a Python program to multiply all the elements of a tuple.Easy - #42String to a tuple of characters
Write a Python program to convert a string into a tuple of characters.Easy - #43Tuple of strings to integers
Write a Python program to convert a tuple of numeric strings to integers, ignoring stray commas.Easy - #44Tuple of digits to a number
Write a Python program to convert a tuple of digits into a number.Easy - #45Element-wise sum of tuples
Write a Python program to add several tuples element by element.Easy - #46List of tuples to list of lists
Write a Python program to convert a list of tuples into a list of lists.Easy - #47Tuples whose items are all divisible by 5
Write a Python program to find the tuples whose elements are all divisible by a number.Easy - #48Tuples with a negative element
Write a Python program to find tuples that contain at least one negative number.Easy - #49Tuples with no negative elements
Write a Python program to find tuples whose elements are all zero or positive.Easy - #50Remove duplicates from a tuple
Write a Python program to remove duplicates from a tuple, keeping the order.Easy - #51Unique digits from tuples
Write a Python program to extract the unique digits of all numbers in a list of tuples.Easy - #52Multiply the ith element of each tuple
Write a Python program to multiply the elements at index i of every tuple.Easy - #53Flatten a list of lists into a tuple
Write a Python program to flatten a list of lists into a tuple.Easy - #54Flatten tuples into a string
Write a Python program to flatten a list of tuples into a space-separated string.Easy - #55Insert a string after every element
Write a Python program to convert a tuple into a list with a string after every element.Easy - #56List of lists to tuple of tuples
Write a Python program to convert a list of lists into a tuple of tuples.Easy - #57Symmetrical tuples
Write a Python program to extract the tuples whose reverse is also in the list.Medium - #58Empty set when nothing is symmetrical
Write a Python program that returns an empty set if no tuples in a list are symmetrical.Medium - #59Remove nested tuples
Write a Python program to remove nested tuples from a tuple.Easy - #60Sort by each tuple's maximum
Write a Python program to sort tuples by their maximum value, largest first.Easy - #61Sort by each tuple's minimum
Write a Python program to sort tuples by their minimum value.Easy - #62Concatenate or nest two tuples
Write a Python program to concatenate two tuples, and to nest them inside one tuple.Easy - #63Order tuples by an external list
Write a Python program to order tuples by the order of their keys in another list.Easy - #64Common tuples ignoring order
Write a Python program to find the tuples that appear in both lists, treating (3, 9) and (9, 3) as the same.Medium - #65Binary tuple to an integer
Write a Python program to convert a tuple of binary digits into an integer.Easy - #66Count unique tuples
Write a Python program to count the distinct tuples in a list.Easy - #67Average of a tuple
Write a Python program to calculate the average of a tuple’s elements.Easy - #68Swap two tuples
Write a Python program to swap two tuple variables.Easy - #69Is the value a tuple?
Write a Python program that returns True if a value is a tuple.Easy - #70Last element with negative indexing
Write a Python program to get the last element of a tuple with negative indexing.Easy