Tuesday, November 26, 2024
Google search engine
HomeLanguagesPython – Convert tuple list to dictionary with key from a given...

Python – Convert tuple list to dictionary with key from a given start value

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))


Output

The 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))


Output

The 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))


Output

The 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)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments