Hey, readers! In this article, we will be focusing on Python List of Tuples. Show What is Python List and Tuple?Python List is a data structure that maintains an ordered collection of mutable data elements. list-name = [ item1, item2, ....., itemN]
The elements in the list are enclosed within square brackets []. Python Tuple is an immutable data structure whose elements are enclosed within parenthesis (). tuple-name = (item1, item2, ...., itemN)
Python List of TuplesWe can create a list of tuples i.e. the elements of the tuple can be enclosed in a list and thus will follow the characteristics in a similar manner as of a Python list. Since, Python Tuples utilize less amount of space, creating a list of tuples would be more useful in every aspect. Example: LT_data = [(1,2,3),('S','P','Q')]
print("List of Tuples:\n",LT_data)
Output: List of Tuples:
[(1, 2, 3), ('S', 'P', 'Q')]
Python list of tuples using zip() functionPython zip() function can be used to map the lists altogether to create a list of tuples using the below command: list(zip(list))
The zip() function returns an iterable of tuples based upon the values passed to it. And, further, the list() function would create a list of those tuples as an output from the zip() function. Example: lst1 = [10,20,30]
lst2 = [50,"Python","JournalDev"]
lst_tuple = list(zip(lst1,lst2))
print(lst_tuple)
Output: [(10, 50), (20, 'Python'), (30, 'JournalDev')]
Customized grouping of elements while forming a list of tuplesWhile forming a list of tuples, it is possible for us to provide customized grouping of elements depending on the number of elements in the list/tuple. [element for element in zip(*[iter(list)]*number)]
List comprehension along with zip() function is used to convert the tuples to list and create a list of tuples. Python iter() function is used to iterate an element of an object at a time. The number would specify the number of elements to be clubbed into a single tuple to form a list. Example 1: lst = [50,"Python","JournalDev",100]
lst_tuple = [x for x in zip(*[iter(lst)])]
print(lst_tuple)
In the above example, we have formed a list of tuples with a single element inside a tuple using the iter() method. Output: [(50,), ('Python',), ('JournalDev',), (100,)]
Example 2: lst = [50,"Python","JournalDev",100]
lst_tuple = [x for x in zip(*[iter(lst)]*2)]
print(lst_tuple)
In this example, two elements are contained inside a tuple to form a list of tuple. Output: [(50, 'Python'), ('JournalDev', 100)]
Python list of tuples using map() functionPython map function can be used to create a list of tuples. The map() function maps and applies a function to an iterable passed to the function. map(function, iterable)
Example: lst = [[50],["Python"],["JournalDev"],[100]]
lst_tuple =list(map(tuple, lst))
print(lst_tuple)
In this example, we have mapped the input list to the tuple function using map() function. After this, the list() function is used to create a list of the mapped tuple values. Output: [(50,), ('Python',), ('JournalDev',), (100,)]
Python list of tuples using list comprehension and tuple() methodPython tuple() method along with List Comprehension can be used to form a list of tuples. The tuple() function helps to create tuples from the set of elements passed to it. Example: lst = [[50],["Python"],["JournalDev"],[100]]
lst_tuple =[tuple(ele) for ele in lst]
print(lst_tuple)
Output: [(50,), ('Python',), ('JournalDev',), (100,)]
ConclusionBy this, we have come to the end of the article. I hope you all have enjoyed learning this interesting concept of Python list of tuples. Feel free to comment below, in case you come across any doubt. References
|