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
#2
Split input string on commas, zip with keys and convert to dict.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Reply
#4
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
 
	..........
	..........	
Reply
#5
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'}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
@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.
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


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,633 Feb-24-2022, 10:40 AM
Last Post: perfringo
  Loop through elements of list and include as value in the dictionary Rupini 3 2,656 Jun-13-2020, 05:43 AM
Last Post: buran
  Fetch student details from a text file and print the details. Pranav 2 6,250 Mar-17-2020, 09:36 AM
Last Post: Pranav
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,046 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  Dictionary/List Homework ImLearningPython 22 10,575 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,610 Oct-24-2018, 06:40 PM
Last Post: LeSchakal
  Need some help with list and dictionary .txt GeekLife97 3 3,827 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