
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create List of Tuples Using For Loop in Python
Python's key data structures are lists and tuples. Once elements of tuples are set they cannot be changed. This is called immutability. But list elements can be modified after initialization. When dealing with data that needs to be grouped together, a for loop is used to create tuple lists. Lists are more adaptable than tuples due to their ability to be modified. This tutorial demonstrates creating a list of tuples using a for loop, simplifying repetitive tasks.
Syntax
for variable in iterable: # loop code
Basic Operations on Tuples
Example
# Initializing my_tuple = (1, 2, "Hello", 3.14) another_tuple = 10, 20, 30 print(another_tuple) # Output: (10, 20, 30) # Get elements my_tuple = (1, 2, 3, 4, 5) print(my_tuple[0]) # Output: 1 print(my_tuple[2]) # Output: 3 # Slicing elements my_tuple = (1, 2, 3, 4, 5) print(my_tuple[1:4]) # Output: (2, 3, 4) # Concatenation tuple1 = (1, 2, 3) tuple2 = (4, 5, 6) combined_tuple = tuple1 + tuple2 # Output: (1, 2, 3, 4, 5, 6) # Tuple Size my_tuple = (1, 2, 3, 4, 5) print(len(my_tuple)) # Output: 5
Output
(10, 20, 30) 1 3 (2, 3, 4) 5
Usage
Tuples are suitable for storing data that should not be modified once created, such as configurations or constant values. Also useful when returning multiple values from a function efficiently.
def get_coordinates(): x = 10 y = 20 return x, y coordinates = get_coordinates() # Output: coordinates = (10, 20) # Can be unpacked into separate variables, making it easy to work with their elements. person = ("John", 30, "Developer") name, age, profession = person # Output: name = "John", age = 30, profession = "Developer" # They are used as keys in dictionaries due to their immutability. my_dict = {("John", 30): "USA", ("Alice", 25): "Canada"}
Due to this flexibility of storage tuples are used in functions and dictionaries. Data integrity is another benefit. A single tuple of any length can be unpacked into multiple variables in a single line of code.
Algorithm
Let an empty list hold the tuples.
Using a for loop, iterate through the elements or objects.
For each entry, create a tuple and append it to the list.
Example 1
Make a list of tuples containing employee names and their corresponding employee IDs from a list of employee names.
employee_names = ["Alice", "Bob", "Charlie", "David", "Eva"] employee_ids = [101, 102, 103, 104, 105] employee_list = [] for i in range(len(employee_names)): employee_list.append((employee_names[i], employee_ids[i]))
Explanation
For tuples, create an empty list named 'employee list'. The for loop iterates through the 'employee names' length range, building tuples with names and IDs. 'employee_list' is added with the freshly formed tuple. This generates a list of tuples holding the lengths of the words in a given phrase.
# Example data sentence = "The quick brown fox jumps over the lazy dog" # Creating a list of tuples using a for loop word_length_list = [(word, len(word)) for word in sentence.split()]
Applications
When working with tabular data, transforming rows to give structure for better data management and analysis.
Tuples enhance database operations by improving data retrieval and administration, and make it easy to do data pairing by merging sources like names and IDs.
Conclusion
Unlike a list, a tuple in Python is an ordered, immutable collection of items. Once created, it cannot be modified. Tuples include a variety of data types, including integers, strings, and floats. This guide has demonstrated how to use a for loop in Python to create a list of tuples. When you wish to construct numerous tuples with varying values, utilizing a for loop to generate a list of tuples might be handy. For loop allows traversal over a list of elements, creating a tuple for each iteration and adding it to the list.