0

Hey i have a question how i can print a specific element in a Dictonary without a loop

i tryed this code but this dosnt work :(

dict = {
    "data": [
        {
            "id": "0",
            "song": "Song5",
            "time": 1660582745,
            "info": "he"
        },
        {
            "id": "1",
            "song": "Song4",
            "time": 1660568345,
            "info": "heheheh"
        }
    ]
}


print(dict["data"]["time"])

i need this output

1660582745, 1660568345

this works but i need it without loop

for i in data["data"]:
    print(i["time"])

output:
    1660582745
    1660568345
petezurich
  • 9,280
  • 9
  • 43
  • 57
Crypto
  • 3
  • 1
  • 1
    One way or another, you're going to need some sort of loop. – Addison Schmidt Aug 15 '22 at 17:50
  • Why do you not want a loop? Do you only always have two items to get? You can get your output using a loop, if that's the actual question. – wkl Aug 15 '22 at 17:51
  • 1
    How could you even design this without a loop, let alone implement it? Unless you literally want to write the same code over and over again except each one having one higher index number. That would be a huge pain though and terrible practice. Does the data absolutely have to be in this format? Is the data's length fixed? If not then even my suggestion wouldn't work – Random Davis Aug 15 '22 at 17:52
  • 1
    is there a reason you dont want to use a loop? – Christian Trujillo Aug 15 '22 at 17:52

3 Answers3

0

Simply try with static list element e.g 0 and 1, if you only always have two items to get then you can do this way, But why don't you want a loop?

print(dict["data"][0]["time"])
print(dict["data"][1]["time"])

But if you have an arbitrary size then this wouldn't be possible.In that case your existing looping way should do the trick.

for item in data["data"]:
    print(item["time"])
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • 1
    It should be pointed out that this will only work for a list with a known size; if it's an arbitrary size then this wouldn't be possible. – Random Davis Aug 15 '22 at 17:54
  • Yes, agreed, going to update the answer to inform that. – A l w a y s S u n n y Aug 15 '22 at 17:54
  • Bacause with the loop i cant enter to the correct index i need but you example helped me :D – Crypto Aug 15 '22 at 17:59
  • @Crypto that doesn't make sense; you need to show an example of why that's the case. A loop has nothing to do with one's ability to index into a list. Simply stating that that's the reason isn't proof enough that what you're saying is actually the case. – Random Davis Aug 15 '22 at 18:02
0

Technically without a "for" loop, using map and operator.itemgetter

(this still iterates the list though) Also don't overwrite builtins such as dict. Here I use my_dict as the name.

from operator import itemgetter

my_dict = { ... }

print(*map(itemgetter("time"), my_dict["data"]))

Result:

1660582745 1660568345

You could also use a comprehension if loops are ok.

[d["time"] for d in my_dict["data"]]
Jab
  • 26,853
  • 21
  • 75
  • 114
0

soo okay its working here the code

json:

{
    "data": [
        {
            "id": "0",
            "song": "Song5",
            "time": "1660582745",
            "info": "he"
        },
        {
            "id": "1",
            "song": "Song4",
            "time": "1660568345",
            "info": "heheheh"
        },
        {
            "id": "2",
            "song": "Song3",
            "time": "1660546745",
            "info": "lololol"
        }
    ]
}

python code

import json
import datetime


def sort_json():
    data = False
    with open('data.json') as json_file:
        data = json.load(json_file)

    now = 1660582985

    temp = []
    sortedlist = []

    for i in data["data"]:
        temp.append(int(i["time"]) - now)

    sortedlist = sorted(temp, key = lambda i: 0 if i == 0 else -1 / i)

    for sort in sortedlist:
        print(data["data"][temp.index(sort)]["song"])


sort_json()
Crypto
  • 3
  • 1
  • I don't understand, why ask with the constraint of not using a loop then answer your question using a loop and that loop is exactly what you said you didn't want? – Jab Aug 15 '22 at 18:04
  • so if i use a loop i need to loop thought sortedlist and inside this loop i need to loop thought the dict with data["data"][temp.index(sort)]["song"] i can directly navigate to the element i need without looping thought the dict – Crypto Aug 15 '22 at 18:06
  • It seems like you asked a very different question from what you wanted solved. – wkl Aug 15 '22 at 18:09
  • im sorry for misunderstanding – Crypto Aug 15 '22 at 18:11