Python Forum
How can details be dynamically entered into a list and displayed using a dictionary?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can details be dynamically entered into a list and displayed using a dictionary?
#1
Please help me out with the below problem. It is mandatory for the input to be in the below format.

INPUT:

Enter number of dictionaries that you want to create
3

First value is a string then three comma separated integers
Arun,75,65,82

First value is a string then three comma separated integers
Ram,85,78,65

First value is a string then three comma separated integers
Shankar,98,85,82

My code is displaying the output in the below format.The key values in the below dict is the iterator.

SAMPLE TEST PROGRAM OUTPUT:
Output:
{0: 'Arun', 1: '75', 2: '65', 3: '82'} {0: 'Ram', 1: '85', 2: '78', 3: '65'} {0: 'Shankar', 1: '98', 2: '85', 3: '82'}
Please help in modifying the code such that the output is displayed in the below format. The dictionary keys should be as below.

DESIRED TEST PROGRAM OUTPUT:

{Name: 'Arun', sub1: '75', sub2: '65', sub3: '82'}
{Name: 'Ram', sub1: '85', sub2: '78', sub3: '65'}
{Name: 'Shankar', sub1: '98', sub2: '85', sub3: '82'}



a=int(input("Enter number of dictionaries that you want to create\n"))

"""Function to take the list values"""
def Creat(l):
    print("First value is a string then three comma separated integers\n")
    l=[str(x) for x in input().split(",")]
    return l

"""Function to store list values in dictionary"""
def Crd(l,d):
    d={}
    for i in range(4):
        kee=i
        val=l[i]
        d[kee]=val
    return d

k=0
while(k<a):
    k=0
    l1=[];l1=Creat(l1)
    d1={}
    d1=Crd(l1,d1)
    k+=1
    if k==a: break

    l2=[];l2=Creat(l2)
    d2={}
    d2=Crd(l2,d2)
    k+=1
    if k==a: break 

    #As per the above process more lists and dictionaries can be created
    l3=[];l3=Creat(l3)
    ....... 
    .......        


z=0
while(z<a):
    z=0
    print(d1)
    z+=1
    if z==a:break

    print(d2)
    z+=1
    if z==a:break

    #As per the above process the outputs of the dictionaries can be printed
    print(d3)
    ....... 
    ....... 
Reply


Messages In This Thread
How can details be dynamically entered into a list and displayed using a dictionary? - by Pranav - Mar-01-2020, 05:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Program that allows to accept only 10 integers but loops if an odd number was entered gachicardo 4 3,717 Feb-24-2022, 10:40 AM
Last Post: perfringo
  Loop through elements of list and include as value in the dictionary Rupini 3 2,706 Jun-13-2020, 05:43 AM
Last Post: buran
  Fetch student details from a text file and print the details. Pranav 2 6,296 Mar-17-2020, 09:36 AM
Last Post: Pranav
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,085 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  Dictionary/List Homework ImLearningPython 22 10,806 Dec-17-2018, 12:12 AM
Last Post: ImLearningPython
  making a dictionary from a list, one key with multiple values in a list within a list rhai 4 3,676 Oct-24-2018, 06:40 PM
Last Post: LeSchakal
  Need some help with list and dictionary .txt GeekLife97 3 3,885 Jan-20-2017, 08:00 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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