Home python python. How to remove curly brackets when displaying a dictionary value?

python. How to remove curly brackets when displaying a dictionary value?

Author

Date

Category

Displays it:

{'meanages': 145}
{'meanages': 112}
{'Messages': 90}
{'Messages': 42}

can somehow remove curly braces and quotes?

UPD:
So I could remove curly brackets, but the quotes remained

print (STR (I [1]) [1: -1] .format ())

Answer 1, Authority 100%

Try this:

users_data_list = [
  [1, {'meanages': 42}],
  [2, {'Messages': 90}],
  [3, {'Messages': 112}],
  [4, {'meanages': 145}],
]
For i in Reversed (Users_Data_List):
  k, v = list (i [1] .Items ()) [0]
  Print (F '{k}: {v: & gt; 3}')

Answer 2, Authority 100%

When you output some object via the print function or call the STR (Object) function , then they are refer to the __ str __ This object.
In this case, i [1] is a dictionary that is standardly displayed as {Key1: Value1, Key2: Value2, ...} .

In order to withdraw the dictionary in a convenient form for you, you need to go through the key-value cycle on all of its pairs, or contact specific values ​​for their keys. Since you always have a dictionary from one element, it can be implemented without a cycle, for example, as follows:

for i in reversed (users_data_list):
  k, v = list (i [1] .Items ()) [0]
  Print (F "{k}: {v}")

Answer 3, Authority 100%

You can make your own dictionary so as not to write every time the cycle:

In [4]: ​​class ReprDict (dict):
  ...: Def __repr __ (Self):
  ...: Return "\ N" .join (f "{k}: {v}" for k, v in self.items ())
  ...:
In [5]: D = REPRDICT ({1: 2, "Hello": "world"})
In [6]: d
Out [6]:
12
hello: world
In [7]: print (d)
12
hello: world

Answer 4

It is possible as

for i in reversed (users_data_list):

for key in i [1]:
  print (i [0], '- & gt;', key, ':', i [1] .get (key))

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions