Replit Database Problems: Converting ObservedList into List

Let’s say I have a list of lists ( ex: [[“apple”, 99, 1], [“orange”, 21, 7], [“banana”, 42, 3]] ) and I want to store it in the replit database. How would I retrieve the data without it returning an ObservedList and get the data as a normal list? In other words, how would I convert the ObservedList into a regular list? I tried using .value, but it returns as a string and not a list. Someone mentioned to_primitive, but there is such little information on the internet about it that I couldn’t figure out how to use it.

:wave: Welcome @jpgdotmp3!

Why do you need to change it back to a normal list? I’m pretty sure they function the exact same.

I can’t add a list to an ObservedList

What do you mean by adding lists?

What I mean by adding lists is merge two lists’ items together like this:
[[“apple”, 99, 1], [“orange”, 21, 7]] + [[“grape”, 44, 6], [“banana”, 42, 3]]
= [[“apple”, 99, 1], [“orange”, 21, 7], [“grape”, 44, 6], [“banana”, 42, 3]]

1 Like

I can’t merge regular lists and ObservedLists into one list, which is why I need to figure out how to convert ObservedLists into just normal lists

Well, I’d imagine you could just use [...] + list(observed_list), but if not, you could try unpacking them instead: [*list, *observed_list]

I tried both ways, but each method prints something like ObservedList(value=['apple', 99, 1]) instead of just ['apple', 99, 1]

print(list(db.get("database"))) outputs [ObservedList(value=['apple', 99, 1]), ObservedList(value=['orange', 21, 7])]

print(*db.get("database")) outputs ObservedList(value=['apple', 99, 1]) ObservedList(value=['orange', 21, 7])

That isn’t what I meant.

What does this output?:

print(list(db.get("database")[0]))

print(list(db.get("database")[0])) outputs ['apple', 99, 1]
I’m pretty sure this fixes my problem thank you bro

You’re welcome. Please mark my post as the solution so anyone with the same issue/question can easily find the answer.

For anyone else who reads this, if you want to convert a database ObservedList into a normal list, use this:

def db_list(key):
  new_list = []
  for i in range (0, len(list(db.get(key)))):
    new_list.append(list(db.get(key)[i]))
  return new_list
1 Like

Hang on, I’m confused? My solution does convert an ObservedList to a normal list. Your solution only works if inside of the db there is a list with another list inside of that one, which is specific to your database, whereas my solution is more general.

Your solution only gets one item
Like I said before, trying to get the whole list at once results in the ObservedList stuff

And like I said before, your solution is more specific whereas mine is more general.

But alright, it is your topic after all.

1 Like

.value is supposed to return the inner object, not a string, and it seems to work that way in a test repl, and also the observed lists/dicts should be functionally the same as actual lists and dicts.

5 Likes

Oh yeah you’re right I just tried it in my repl and it worked perfectly fine
I’m not sure why I got a string before, but .value is way faster
Thank you for letting me know about this

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.