Python Forum
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"'}]
Reply
#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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 1,452 Sep-03-2022, 11:21 PM
Last Post: Winfried
  Reading list items without brackets and quotes jesse68 6 4,524 Jan-14-2022, 07:07 PM
Last Post: jesse68
  Data pulled from SQL comes in brackets nickzsche 3 2,549 Jan-04-2022, 03:39 PM
Last Post: ibreeden
  For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game new_coder_231013 7 2,170 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013
  Dictionnary indexing error Ander 6 1,833 Sep-10-2021, 12:22 PM
Last Post: Ander
  Getting a certain value from inside brackets. LeoT 5 2,941 Mar-01-2021, 03:34 PM
Last Post: buran
  Removing internal brackets from a string Astrikor 4 2,618 Jun-04-2020, 07:54 PM
Last Post: Astrikor
  Taking brackets out of list in print statement pythonprogrammer 3 2,340 Apr-13-2020, 12:25 PM
Last Post: perfringo
  printing a list contents without brackets in a print statement paracelx 1 2,081 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  python dictionnary Omar_Yatim 3 2,757 Dec-08-2019, 05:12 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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