Python Forum
Create Dict from multiple Lists with duplicate Keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create Dict from multiple Lists with duplicate Keys
#1
I have four lists named designName,creatorName,fabric_names and data the data list consist of multiple duplicate values which I want to map it to it's asscociated design name and creator name like:

Output:
Catching Fireflies thestorysmith FABRIC_PETAL_SIGNATURE_COTTON 1.75 10.58 18.22 Catching Fireflies thestorysmith FABRIC_SATIN 1.75 11.85 19.71 Catching Fireflies thestorysmith FABRIC_COTTON_POPLIN_BRAVA 1.75 11.85 19.71.... Spoonflower Color Map spoonflower_help FABRIC_PETAL_SIGNATURE_COTTON N/A N/A 16.4 Spoonflower Color Map spoonflower_help FABRIC_SATIN N/A N/A 17.74 Spoonflower Color Map spoonflower_help FABRIC_COTTON_POPLIN_BRAVA N/A N/A 17.74... Night Sky Stars Midnight Blue at_the_cottage FABRIC_PETAL_SIGNATURE_COTTON 1.75 10.58 18.22 Night Sky Stars Midnight Blue at_the_cottage FABRIC_SATIN 1.75 11.85 19.71 Night Sky Stars Midnight Blue at_the_cottage FABRIC_COTTON_POPLIN_BRAVA 1.75 11.85 19.71
Data:

desigName = ['Catching Fireflies', 'Spoonflower Color Map', 'Night Sky Stars Midnight Blue']
creatorName = ['thestorysmith', 'spoonflower_help', 'at_the_cottage']
fabric_names = ['FABRIC_PETAL_SIGNATURE_COTTON', 'FABRIC_SATIN', 'FABRIC_COTTON_POPLIN_BRAVA']
data = [('FABRIC_PETAL_SIGNATURE_COTTON', 1.75, 10.58, 18.22),('FABRIC_PETAL_SIGNATURE_COTTON', 1.75, 10.58, 18.22),('FABRIC_PETAL_SIGNATURE_COTTON', 1.75, 10.58, 18.22),... ('FABRIC_SATIN', 1.75, 11.85, 19.71),('FABRIC_SATIN', 1.75, 11.85, 19.71),('FABRIC_SATIN', 1.75, 11.85, 19.71),... ('FABRIC_COTTON_POPLIN_BRAVA', 1.75, 11.85, 19.71),('FABRIC_COTTON_POPLIN_BRAVA', 1.75, 11.85, 19.71),('FABRIC_COTTON_POPLIN_BRAVA', 1.75, 11.85, 19.71),...]
I am trying to figure out the best way to turn this data into one ordered dict like the following.

Output:
{('Catching Fireflies', 'thestorysmith'): {'fabric_name_00': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_00': 1.75, 'fat_quarter_meter_00': 10.58, 'meter_00': 18.22, 'fabric_name_01': 'FABRIC_SATIN', 'test_swatch_meter_01': 1.75, 'fat_quarter_meter_01': 11.85, 'meter_01': 19.71, 'fabric_name_02': 'FABRIC_COTTON_POPLIN_BRAVA', 'test_swatch_meter_02': 1.75, 'fat_quarter_meter_02': 11.85, 'meter_02': 19.71}}
I've tried so far:
for fab in fabric_names:
        print(fab)
    for name, creator in zip(designName, creatorName):
        for fab_type in fabric_names:
            Design_Name = name
            Creator_Name = creator
            test_swatch_meter = data[1]
            fat_quarter_meter = data[2]
            meter = data[3]

            if (name, creator) not in items_dict.keys():
                items_dict[(name, creator)] = {}
            itemCount = len(items_dict[(name, creator)].values()) / 4
            items_dict[(name, creator)].update({'fabric_name_%02d' %itemCount: fab_type,
            'test_swatch_meter_%02d' %itemCount: test_swatch_meter,
            'fat_quarter_meter_%02d' %itemCount: fat_quarter_meter,
            'meter_%02d' %itemCount: meter})
but cannot get it to format like above.
Reply


Messages In This Thread
Create Dict from multiple Lists with duplicate Keys - by rhat398 - Jun-23-2021, 11:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,626 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Find duplicate files in multiple directories Pavel_47 9 3,208 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  Create multiple/single csv file for each sql records mg24 6 1,451 Sep-29-2022, 08:06 AM
Last Post: buran
  Updating nested dict list keys tbaror 2 1,306 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Loop Dict with inconsistent Keys Personne 1 1,626 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,871 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Better way to append frames from a video to multiple lists? Balaganesh 0 1,861 May-13-2021, 07:37 AM
Last Post: Balaganesh
  Xlsxwriter: Create Multiple Sheets Based on Dataframe's Sorted Values KMV 2 3,541 Mar-09-2021, 12:24 PM
Last Post: KMV
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,997 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Create a sequential number (counter) and reset at each duplicate Mekala 0 1,758 Sep-20-2020, 05:02 AM
Last Post: Mekala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020