-1

i have a json data like

    "preference":[
                {
                  "access":"true1",
                  "visibility":"false1"
                },
                {
                  "access":"true2",
                  "visibility":"false2"
                },
                {
                  "access":"true3",
                  "visibility":"false3"
                },
           ]

i want to access the list objects and display every list element in table format

    for item in data['preference']
         print(item)
      
     TableData = [
             [item.get('access'),item.get('visibility')]
             ]

this prints only the last item

           {
              "access":"true3",
              "visibility":"false3"
            },

in table format please suggest me some code to access every element in table format asap Thank you in Advance..

  • 1
    I can't understand the question. Please fix your formatting (indentation is meaningful in Python) and show the exact expected output as well as the actual output. – Karl Knechtel Aug 30 '21 at 06:17
  • Because ```item``` is assigned the last value –  Aug 30 '21 at 06:18
  • ```TableData = [[item.get('access'),item.get('visibility')]for item in data['preference']]```? –  Aug 30 '21 at 06:19
  • "access" is for retrieing data, "in table format" is for outputting data – azro Aug 30 '21 at 06:21
  • Fix your code indention *and* use correct upper case letters. Help the helpers to help you. https://stackoverflow.com/help/how-to-ask – buhtz Sep 08 '21 at 09:42

2 Answers2

0

If you want to only display the data, you don't any container to save the data

for item in data['preference']:
    print(f"{item.get('access')} | {item.get('visibility')}")

But if you want to sav them, use a list around

# FIX YOUR COE
TableData = []
for item in data['preference']:
    TableData.append([item.get('access'), item.get('visibility')])

# IMPROVE
TableData = [[item.get('access'), item.get('visibility')] for item in data['preference']]

print(TableData)  # [['true1', 'false1'], ['true2', 'false2'], ['true3', 'false3']]
azro
  • 53,056
  • 7
  • 34
  • 70
0

The reason for this behavior is you are not appending to the TableData list, you are just adding it, so as you go on adding the element to the list it overwrites the last element.
Here is the code where you will get the expected output:

data = {
"preference":[
                {
                  "access":"true1",
                  "visibility":"false1"
                },
                {
                  "access":"true2",
                  "visibility":"false2"
                },
                {
                  "access":"true3",
                  "visibility":"false3"
                },
           ]
}
TableData = []
for item in data['preference']:
    print(item)
    TableData.append([item.get('access'),item.get('visibility')])
    
print (TableData)

Output:

[['true1', 'false1'], ['true2', 'false2'], ['true3', 'false3']]
Bhagyesh Dudhediya
  • 1,800
  • 1
  • 13
  • 16