Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionnary Issue
#1
Hi, little issue. Could u please tell me how to remove the {} between each number and "0" in the output of the console ? Thanks!
def fonction2_bis(n,a,b):
    import random
    r= random.randint(a,b)
    L=[]
    L2=[]
    for i in range(n):
        r= random.randint(a,b)
        L.append(r)
    for item in L:
        dict={str(item): "\""+str(0) +"\""}
        L2.append(dict)
    print("There are",(len(L)), "items")
    return L2
Output:
>>> fonction2_bis(5,1,2) There are 5 items [{'2': '"0"'}, {'2': '"0"'}, {'2': '"0"'}, {'2': '"0"'}, {'1': '"0"'}]
Reply
#2
what is your intented output?
[2,0,2,0,2,0,2,0,1,0]
or
[2,0,1,0]

Your brackets are being created on this line so you can just recreated this to remove it
        dict={str(item): "\""+str(0) +"\""}
Recommended Tutorials:
Reply
#3
L2 is a list of dicts. You're not currently using the values of the dicts for anything, so why not make L2 a list instead?

So instead of:
    for item in L:
        dict={str(item): "\""+str(0) +"\""}
        L2.append(dict)
Try this:
    for item in L:
        L2.append(str(item))
Reply


Forum Jump:

User Panel Messages

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