Tuples are a type of variable that allows you to store multiple items in a single variable. Tuple is one of four built-in data types in Python that are used to store data collections. The other three are List, Set, and Dictionary, all of which have different qualities and applications. A tuple is a collection that is both ordered and immutable.
In this post we are going to discuss different ways to create the tuple and traverse through it.
Create a Tuple and Traverse it
- List vs Tuple
- Create a tuple of various types of elements
- Create a tuple out of unpacked elements
- Create empty tuple
- Convert list to tuple
- Traverse the tuple
1)List vs Tuple
- Lists and tuples are represented in slightly different ways. Lists are commonly surrounded by the square bracket [], and elements are separated by commas. Tuples are surrounded by parenthesis (), and elements are separated by a comma. The parenthesis is optional, and these tuples are known as tuple packing.
- It is the most significant distinh3ction between a list and a tuple, with lists being mutable and tuples being immutable. Lists are mutable, which means that the Python object can be modified after it is created, whereas tuples cannot be modified after they are created.
- Tuples support fewer operations than lists. The built-in dir(object) function is used to retrieve all of the list and tuple’s supported functions.
2)Create a tuple of various types of elements
We can make a tuple of different elements by separating them with ‘,’ and wrapping them in braces i.e ( ).
Below is the implementation:
# Tuple newtuple = ('hello', 3.5, 'this', 200, 'is', True, 'BTechGeeks') # print tuple print(newtuple)
Output:
('hello', 3.5, 'this', 200, 'is', True, 'BTechGeeks')
- Python : How to Find an Element in Tuple by Value
- Python Program to Return Multiple Values From a Function
- Python Program to Shuffle Elements of a Tuple
3)Create a tuple out of unpacked elements
We can also create a tuple from unpacked elements i.e without any braces just elements separated by comma.
Below is the implementation:
# Tuple newtuple = 'hello', 3.5, 'this', 200, 'is', True, 'BTechGeeks' # print tuple print(newtuple)
Output:
('hello', 3.5, 'this', 200, 'is', True, 'BTechGeeks')
4)Create empty tuple
We can create tuple directly using () symbol.
Below is the implementation:
# creating new tuple newtuple = () # print tuple print(newtuple)
Output:
()
5)Convert list to tuple
We can convert list to tuple by using the syntax tuple(list_name).
Below is the implementation:
# given list givenlist = [100, 'hello', 4.5, 'BTechGeeks'] # converting this list to tuple newtuple = tuple(givenlist) # print new tuple print(newtuple)
Output:
(100, 'hello', 4.5, 'BTechGeeks')
6)Traverse the tuple
We can iterate over the tuple using for loop.
Below is the implementation:
# Tuple newtuple = ('hello', 3.5, 'this', 200, 'is', True, 'BTechGeeks') # Traverse the tuple for i in newtuple: print(i)
Output:
hello 3.5 this 200 is True BTechGeeks
Related Programs:
- python different ways to iterate over a list in reverse order
- pandas 6 different ways to iterate over rows in a dataframe update while iterating row by row
- what is a structured numpy array and how to create and sort it in python
- different ways to iterate over a list of objects in cpp
- different ways to iterate over a set in cpp
- python program to create a class in which one method accepts a string from the user and another prints it
- how to create and initialize a list of lists in python