Python - Convert tuple list to dictionary with key from a given start value
Last Updated : 28 Apr, 2023
Given a tuple list, the following article focuses on how to convert it to a dictionary, with keys starting from a specified start value. This start value is only to give a head start, next keys will increment the value of their previous keys.
Input : test_list = [(4, 5), (1, 3), (9, 4), (8, 2), (10, 1)], start = 4 Output : {4: (4, 5), 5: (1, 3), 6: (9, 4), 7: (8, 2), 8: (10, 1)} Explanation : Tuples indexed starting key count from 4.
Input : test_list = [(4, 5), (1, 3), (9, 4), (8, 2), (10, 1)], start = 6 Output : {6: (4, 5), 7: (1, 3), 8: (9, 4), 9: (8, 2), 10: (10, 1)} Explanation : Tuples indexed starting key count from 6.
Method 1 : Using loop
In this, we construct the dictionary by iterating through each tuple and adding its position index, starting from start, as key-value pair in the dictionary.
Python3 # initializing list test_list = [(4, 5), (1, 3), (9, 4), (8, 2), (10, 1)] # printing original list print("The original list is : " + str(test_list)) # initializing start start = 4 res = dict() for sub in test_list: # assigning positional index res[start] = sub start += 1 # printing result print("Constructed dictionary : " + str(res))
OutputThe original list is : [(4, 5), (1, 3), (9, 4), (8, 2), (10, 1)] Constructed dictionary : {4: (4, 5), 5: (1, 3), 6: (9, 4), 7: (8, 2), 8: (10, 1)}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using dict() and enumerate()
In this, we convert tuple list to dictionary using dict(), and indexing is provided using enumerate().
Python3 # initializing list test_list = [(4, 5), (1, 3), (9, 4), (8, 2), (10, 1)] # printing original list print("The original list is : " + str(test_list)) # initializing start start = 4 res = dict(enumerate(test_list, start=start)) # printing result print("Constructed dictionary : " + str(res))
OutputThe original list is : [(4, 5), (1, 3), (9, 4), (8, 2), (10, 1)] Constructed dictionary : {4: (4, 5), 5: (1, 3), 6: (9, 4), 7: (8, 2), 8: (10, 1)}
Time Complexity: O(n)
Auxiliary Space: O(n)
Using itertools.count to create an iterator for the keys:
test_list: a list of tuples
start: the starting index to use for the keys in the resulting dictionary
The function uses the zip function and the itertools.count function to create a dictionary where the keys start at start and increase by 1 for each tuple in test_list, and the values are the tuples themselves.
Python3 import itertools def tuple_list_to_dict(test_list, start): res_dict = dict(zip(itertools.count(start), test_list)) return res_dict # Example usage 1 test_list = [(4, 5), (1, 3), (9, 4), (8, 2), (10, 1)] start = 4 result = tuple_list_to_dict(test_list, start) print(result) # Output : {4: (4, 5), 5: (1, 3), 6: (9, 4), 7: (8, 2), 8: (10, 1)} # Example usage 2 test_list = [(4, 5), (1, 3), (9, 4), (8, 2), (10, 1)] start = 6 result = tuple_list_to_dict(test_list, start) print(result) # Output : {6: (4, 5), 7: (1, 3), 8: (9, 4), 9: (8, 2), 10: (10, 1)}
Output{4: (4, 5), 5: (1, 3), 6: (9, 4), 7: (8, 2), 8: (10, 1)} {6: (4, 5), 7: (1, 3), 8: (9, 4), 9: (8, 2), 10: (10, 1)}
Time complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using a list comprehension with zip()
Python3 # initializing list test_list = [(4, 5), (1, 3), (9, 4), (8, 2), (10, 1)] # printing original list print("The original list is: " + str(test_list)) # initializing start start = 4 # using list comprehension with zip to create dictionary res = {start+i: pair for i, pair in enumerate(test_list)} # printing result print("Constructed dictionary: " + str(res))
OutputThe original list is: [(4, 5), (1, 3), (9, 4), (8, 2), (10, 1)] Constructed dictionary: {4: (4, 5), 5: (1, 3), 6: (9, 4), 7: (8, 2), 8: (10, 1)}
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python - Convert key-values list to flat dictionary
We are given a list that contains tuples with the pairs of key and values we need to convert that list into a flat dictionary. For example a = [("name", "Ak"), ("age", 25), ("city", "NYC")] is a list we need to convert it to dictionary so that output should be a flat dictionary {'name': 'Ak', 'age':
3 min read
Python - Convert List to Index and Value dictionary
Given a List, convert it to dictionary, with separate keys for index and values. Input : test_list = [3, 5, 7, 8, 2, 4, 9], idx, val = "1", "2" Output : {'1': [0, 1, 2, 3, 4, 5, 6], '2': [3, 5, 7, 8, 2, 4, 9]} Explanation : Index and values mapped at similar index in diff. keys., as "1" and "2". Inp
3 min read
Convert List of Tuples to Dictionary Value Lists - Python
The task is to convert a list of tuples into a dictionary where the first element of each tuple serves as the key and the second element becomes the value. If a key appears multiple times in the list, its values should be grouped together in a list. For example, given the list li = [(1, 'gfg'), (1,
4 min read
Python - Convert String List to Key-Value List dictionary
Given a string, convert it to key-value list dictionary, with key as 1st word and rest words as value list. Input : test_list = ["gfg is best for geeks", "CS is best subject"] Output : {'gfg': ['is', 'best', 'for', 'geeks'], 'CS': ['is', 'best', 'subject']} Explanation : 1st elements are paired with
8 min read
Python - Tuple key detection from value list
Sometimes, while working with record data, we can have a problem in which we need to extract the key which has matching value of K from its value list. This kind of problem can occur in domains that are linked to data. Lets discuss certain ways in which this task can be performed. Method #1 : Using
6 min read
Convert List to Single Dictionary Key - Value list - Python
We are given a list and a element K, our aim is to transform the given list into a dictionary where the specified element (Kth element) becomes the key and the rest of the elements form the value list. For example: if the given list is: [6, 5, 3, 2] and K = 1 then the output will be {5: [6, 3, 2]}.
4 min read
Convert key-value pair comma separated string into dictionary - Python
In Python, we might have a string containing key-value pairs separated by commas, where the key and value are separated by a colon (e.g., "a:1,b:2,c:3"). The task is to convert this string into a dictionary where each key-value pair is represented properly. Let's explore different ways to achieve th
3 min read
Convert Dictionary to String List in Python
The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list. For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a st
3 min read
Update a Dictionary with the Values from a Dictionary List
The task of updating a dictionary with values from a list of dictionaries involves merging multiple dictionaries into one where the keys from each dictionary are combined and the values are updated accordingly. For example, we are given an dictionary d = {"Gfg": 2, "is": 1, "Best": 3} and a list of
4 min read
Convert List of Dictionary to Tuple list Python
Given a list of dictionaries, write a Python code to convert the list of dictionaries into a list of tuples.Examples: Input: [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] Output: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)] Below are various methods to co
5 min read