Python list comprehension list of tuples
Python Create a List of TuplesIn this article, we will discuss multiple ways by which we can create a list of tuples in Python. Show Method 1: Using list() and tuple() methodswe can create a list of tuples using list and tuples directly. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course Syntax:
Example: Python code to create list of tuples using list and tuple Python3
Output:
Method 2: Using zip() functionUsing the zip() function we can create a list of tuples from n lists. Syntax:
Here, lists are the data (separate lists which are elements like tuples in the list Example: Python program to create two lists with college id and name and create a list of tuples using zip() function Python3
Output:
Method 3: Using zip() and iter() methodHere we are going to form a list of tuples using iter() function along with zip() function. Syntax:
where x is the iterator to iterate in the list, zip is used to zip the list and iter() is used to iterate through the entire list Example: Python code to create a list of tuples by forming a list of tuples Python3
Output:
Method 4: using map() functionHere we are passing the data in list and then using the map() function we can create a list of tuples Syntax:
Here, list_data is the input list to create a list of tuples, list is a predefined function and tuple is a predefined function Example: Python code to create a list of tuples from the list using map() function Python3
Output:
Method 5: Using list comprehension and tuple() methodHere we are using comprehension and tuple to create a list of tuples. Syntax:
where tuple(x) is an iterator to convert iterative objects to tuple and list_data is the input data Example: Python code to create a list of tuples using list comprehension and tuple() method Python3
Output:
Article Tags :
Python
Python List-of-Tuples python-list python-tuple Practice Tags :
python-list
Read Full Article
|