简体   繁体   中英

How can i remove brackets from the output of dictionary

In the output the values are printed with square brackets how to remove them?

Test= {'Python':['classes','labs','tutorial'], 'Math': 
('classes','tutorial'), 'Java':['classes',{'labs':['corejava','adv 
java']}]}

for subject, details in Test.items(): 
     print(subject, "->", details)


print("after updating...")   

Test2 = {'Math':('classes','tutorial','labs')}
Test.update(Test2)
for subject, details in Test.items(): 
    print(subject, "->", details) 

This is how I would I do it for this particular case:

Test= {'Python':['classes','labs','tutorial'], 'Math': 
('classes','tutorial'), 'Java':['classes',{'labs':['corejava','adv java']}]}

for subject, details in Test.items(): 
     print(subject, "->", details)


print("after updating...")   

Test2 = {'Math':('classes','tutorial','labs')}
Test.update(Test2)
for subject, details in Test.items(): 
    try:
        print(subject, "->", ' '.join(details)) 
    except TypeError:
        print(subject, "->", ''.join(details[0]),''.join(list(details[1])),' '.join(details[1]['labs']))

Output:

Python -> ['classes', 'labs', 'tutorial']
Math -> ('classes', 'tutorial')
Java -> ['classes', {'labs': ['corejava', 'adv java']}]
after updating...
Python -> classes labs tutorial
Math -> classes tutorial labs
Java -> classes labs corejava adv java

HOWEVER

You are seeing the value in brackets, because that's how lists are displayed, and tuples are displayed between parenthesis. If you wish to eliminate the lists, then you can either print each value individually and append it to a string, or turn the whole list into one big string and eliminate the brackets. In the final 3 lines I added this:

for subject, details in Test.items(): 
    print(subject, "->", details)
    print(type(details)) 

Which resulted in the output I talked about before:

Python -> ['classes', 'labs', 'tutorial']
<class 'list'>
Math -> ('classes', 'tutorial', 'labs')
<class 'tuple'>
Java -> ['classes', {'labs': ['corejava', 'adv java']}]
<class 'list'>

If you don't want any brackets, you can loop through individual keys and print them. Here is one of the options

Test= {'Python':['classes','labs','tutorial'], 'Math':  ('classes','tutorial'), 'Java':['classes',{'labs':['corejava','adv java']}]}

for subject, details in Test.items():
    print('\n'+ subject, "->", end=' ')
    for details in details:
        if( isinstance(details,dict)):
            for subject, details in details.items():
                print('\n'+ subject, "->", end=' ')
                if(isinstance(details,dict)):
                    for subject, details in details.items():
                        print('\n'+ subject, "->", end=' ')
                        print(' '.join(details))
                else:
                    print(' '.join(details))
        else:
            print(details, end=' ')

Update: Considered inner dicts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM