Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 12 Nov, 2020

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

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!