How to put the array values from the dictionary into a new array?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lt

Hello! I am a beginner. I have a dictionary (all values are arrays) I want to put the values from the A B C D array [0] into a new array. I did get the result I wanted. But I don’t think this is a good way to do it. Because the number of arrays in other dictionaries may be different.
I think I should use a for loop or something to get the result, my attempt failed. Please give me some good advice. Thanks!

extends Node

var dict = {
        "A": [1,    2,    3,    4],
		"B": [12,   4,    3,    8],
		"C": [true, true, false,true],
		"D": ["L1","L2", "L3", "L4"],
		}

func _ready():
    var arr= dict.values() #Puts all the dictionary into an array
    var num= max(dict.size(),dict.size()) #There are 4 arrays in dict

    #(funny method)
    var new_arr = [arr[0][0],arr[1][0],arr[2][0],arr[3][0]]
    prints(new_arr)
    return new_arr #print:[1, 12, True, L1]-- But.

I am confused, why are you trying to copy your dictionary? Why not simply get the values directly from the dictionary?

Gluon | 2022-11-11 14:19

what is the game about and is it 2d or 3d ? I didn’t understand you well i am also beginner

dbldbli | 2022-11-11 14:59

This is COMMENT not an ANSWER (and, it’s irrelevant to the question). Please use the appropriate function when responding…

jgodfrey | 2022-11-11 15:26

Why are you so angry? relax man i just ask you about the game

dbldbli | 2022-11-12 17:05

First, I’m not angry (and, I’m not the OP).

I’m just trying to point out the proper use of the forum. When you use the “Answer” feature (like you did), the question shows up in the main forum as having “been answered”. Due to the amount of traffic on the forum, those of us who attempt to answer questions don’t tend to spend much time looking at questions that have already been answered.

So, your input (posted using the “Answer” feature) could have easily caused this question to never receive an actual answer - thus doing a disservice to the original poster.

So, in the future, just use the “Comment” feature when seeking additional information about the question.

jgodfrey | 2022-11-12 17:29

thanks dude for teaching me this because i am new user and i didn’t knew this before i am not mocking i am serious and i didn’t know that will hurt you I am sorry :slight_smile:
i am trying to delete the answer

dbldbli | 2022-11-13 12:50

This is the comment section not the answer section. You are telling him to post a comment and not an answer on his comment.

Gluon | 2022-11-14 00:08

This is the comment section not the answer section.

Not much to be gained here now, but…

Originally, the below statement (now shown above as a comment) was submitted as an Answer:

what is the game about and is it 2d or 3d ? I didn’t understand you well i am also beginner

Once I explained the Comment / Answer system, the OP apparently used the “Hide” feature to remove the answer. I’m not sure what happened from there. I can only guess the forum software took the conversation belonging to the now Hidden answer and moved it to the comment section of the OP - hence it looks like a Comment now and the entire conversation makes no sense… :frowning:

jgodfrey | 2022-11-14 00:20

Yes, but the comment was in answer section. :slight_smile:

dbldbli | 2022-11-14 13:35

never mind i can understand you , just tell me is it 2d or 3d
in 3d it will be like this

but in 2d it will be like this

dbldbli | 2022-11-14 13:46

:bust_in_silhouette: Reply From: jgodfrey

Here’s one way…

func _ready() -> void:
	var new_array = []
	var dict = {
		"A": [1,    2,    3,    4],
		"B": [12,   4,    3,    8],
		"C": [true, true, false,true],
		"D": ["L1","L2", "L3", "L4"],
	}
	var arrays = dict.values()
	for array in arrays:
		new_array.append(array[0])
	print(new_array)

This is very good way. Thanks!!

lt | 2022-11-11 16:33

:bust_in_silhouette: Reply From: Wakatta

Set pos to the point you want to query

var pos = 0
var new_arr = []
for value in dict:
	if dict[value].size() > pos:
	   new_arr.append(dict[value][pos])