Python Forum
How can details be dynamically entered into a list and displayed using a dictionary? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How can details be dynamically entered into a list and displayed using a dictionary? (/thread-24719.html)



How can details be dynamically entered into a list and displayed using a dictionary? - Pranav - Mar-01-2020

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



RE: How can details be dynamically entered into a list and displayed using a dictionary? - perfringo - Mar-01-2020

Split input string on commas, zip with keys and convert to dict.


RE: How can details be dynamically entered into a list and displayed using a dictionary? - ibreeden - Mar-01-2020

First question: what is a dictionary? A dictionary is a structure with name - value pairs. In your assignment it appears the keys are fixed. The keys must be: "Name, "sub1", "sub2" and "sub3".
So start easy. You already have a function to make a list of values. You only have to link these values to the keys. Like this:
ll = ["Arun", 75, 65, 82]
dd = {}
dd["Name"] = ll[0]
dd["sub1"] = ll[1]
dd["sub2"] = ll[2]
dd["sub3"] = ll[3]

print(dd)
Output:
{'Name': 'Arun', 'sub1': 75, 'sub2': 65, 'sub3': 82}
Now try to incorporate this in your program. When that works also look at the hint of Perfringo. His solution is more beautiful. But first try to make it work as simple as possible.


RE: How can details be dynamically entered into a list and displayed using a dictionary? - Pranav - Mar-02-2020

Thanks for your response. Your solution was simple and effective.
Below code is working fine.

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={}
    d["Name"] = l[0]; d["sub1"] = l[1]; d["sub2"] = l[2];d["sub3"] = l[3]
    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 
    
	..........
	..........	
 
z=0
while(z<a):
    z=0
    print(d1)
    z+=1
    if z==a:break
 
    print(d2)
    z+=1
    if z==a:break
 
	..........
	..........	



RE: How can details be dynamically entered into a list and displayed using a dictionary? - perfringo - Mar-02-2020

Another way using Python built-in tools:

>>> row = input('Enter database row: ').split(',')
Enter database row: First,1,2,3
>>> keys = ['name', 'sub1', 'sub2', 'sub3']
>>> d = dict(zip(keys, row)) 
>>> d                                                                                                                                        
{'name': 'First', 'sub1': '1', 'sub2': '2', 'sub3': '3'}



RE: How can details be dynamically entered into a list and displayed using a dictionary? - buran - Mar-02-2020

@Pranav - you code show basic confusion about use of function arguments.
Your functions take arguments, but in 2 out of 3 cases you are not using them properly.
Look at function Creat. It takes argument l (by the way poor choice of name for couple of reasons, but I will come to that at the end of my post). Then in the function body you don't use that l, you simply overvrite it in the list comprehension. When you call the function on line 18, you just pass empty list. The function will work all the same without any arguments.
def Creat():
    print("First value is a string then three comma separated integers\n")
    my_list =[str(x) for x in input().split(",")]
    return my_list
or even

def Creat():
    return [str(x) for x in input("First value is a string then three comma separated integers\n").split(",")]
same apply to argument d in your other function. In it however the use of l is correct - you pass argument and use it in the function.

Now, about other problems in your code
  • Don't use single letter names. Descriptive names are your friend and make it easier to maintain code in long-term.
  • If you use single letter for some reason, avoid names like l (lowercase L), o (lowercase O). It's difficult to distinguish lowercase L and 1 (one). Also lowercase O and zero. You can see it's difficult to tell if it is ll or l1
  • Avoid having multiple statements separated by ;. It's uncommon in python although syntax is valid.