Python Forum
Displaying duplicates in dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Displaying duplicates in dictionary
#1
Hi,

I am trying to below snippet of code but displaying duplicates. Can you please let me know where i did mistake.

src_base_path="/data/infy"
year_mth="2020-10-05"
folder_names = "1, 2, 3"
table_names = "bt1, bt2, bt3"

src_path = '{0}/{1}/app_date={2}/source={3}/'
input_dict = {}
input_body = []
tables = list(table_names.split(', '))
print("Tables to be processed: {}".format(tables))
folders = list(folder_names.split(', '))
print("Folders to be processed: {}".format(folders))
for i in range(len(folders)):
    for j in range(0,len(tables)):
        input_body.append({'sourcePath': src_path.format(src_base_path, tables[j], year_mth, folders[i]), 'tableName': tables[j]})
input_dict['tables'] = input_body
print("Input_body: {}".format(input_dict))
print(input_dict)
Thanks in advance
Reply
#2
because we don't see your data, would you explain in more detail - what duplicates, what is expected and what i the actual output,etc.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
It's impossible to have with your code duplicates, because you're not creating duplicates.
I changed your code a litte bit (functionality is the same):

from pprint import pprint


src_base_path = "/data/infy"
year_mth = "2020-10-05"
folder_names = ["1", "2", "3"]
table_names = ["bt1", "bt2", "bt3"]

print(f"Tables to be processed: {table_names}")
print(f"Folders to be processed: {folder_names}")

src_path = "{0}/{1}/app_date={2}/source={3}/"
input_body = []
for folder_name in folder_names:
    for table_name in table_names:
        input_body.append(
            {
                "sourcePath": src_path.format(
                    src_base_path, table_name, year_mth, folder_name
                ),
                "tableName": table_name,
            }
        )


pprint(input_body)
A dict can have only unique keys. Putting stuff into a dict with the same key will overwrite the old value.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove partial duplicates from csv ledgreve 0 788 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Problem : Count the number of Duplicates NeedHelpPython 3 4,379 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  Removal of duplicates teebee891 1 1,795 Feb-01-2021, 12:06 PM
Last Post: jefsummers
  Dictionary iteration and creation a new dictionary from duplicates xrsxlnx 2 2,130 Mar-30-2020, 10:43 AM
Last Post: xrsxlnx
  how do i pass duplicates in my range iterator? pseudo 3 2,356 Dec-18-2019, 03:01 PM
Last Post: ichabod801
  Deleting duplicates in tuples Den 2 2,758 Dec-14-2019, 10:32 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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