Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Tuple key detection from value list
Next article icon

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

Last Updated : 28 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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)


Next Article
Python - Tuple key detection from value list
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences