Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Dictionnary brackets 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"'}]
Quote
#2
You can't. That's the return value. The return value is a list of dictionaries. That's what a list of dictionaries looks like. If you want to print that return value in a way that does not have those brackets, you can do that. But you would need to to loop through the values in that list, constructing a string representation of the list that did not have those brackets. Something like print(', '.join(str(data)[1:-1] for data in return_value)) would do that.
newbieAuggie2019 likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Access to the elements of a dictionnary Reims 1 118 Oct-02-2019, 12:48 PM
Last Post: SheeppOSU
  from Json Time Serie file to python dictionnary Reims 1 131 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  convert a json file to a python dictionnary of array Reims 2 143 Sep-10-2019, 01:08 PM
Last Post: Reims
  dictionnary lateublegende 1 260 Apr-29-2019, 09:10 PM
Last Post: Yoriz
  Why do we need setdefault() method for dictionnary? DJ_Qu 3 344 Apr-21-2019, 11:00 AM
Last Post: Gribouillis
  Json dictionnary on Redis katsu707 1 386 Dec-04-2018, 11:59 AM
Last Post: DeaD_EyE
  how to remove brackets within a list A3G 17 6,459 Oct-16-2016, 02:34 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)