How to iterate over a dictionary of arrays (without knowing the key name)

python dictionary
iterate through dictionary python
iterate list of dictionaries python
iterate through nested dictionary python
python iterate dictionary key, value
while loop dictionary python
append to dictionary python
python iterate through dictionary of tuples

I have a an object that looks similar to this:

[
    {"key1": ["2019-04-12-14:54:29.190", "19", "0", "4325", "1"]},
    {"key2": ["2019-04-12-14:54:29.191", "20", "0", "2212", "1"]},
    {"key3": ["2019-04-12-14:54:29.192", "22", "0", "4376", "0"]}
]

However I do NOT know the names of the keys (ie key1, key2 and key3 names are not known to me).

This post is almost exactly what I want, except that method requires you to know the names of the keys.

I need to be able to iterate over the key name and it's value array.

I've tried this:

for (var i in zk) {
    for (var j in zk[i]) {
        console.log(j)
    }
}

But that only prints the key names. How can I iterate through the list as well? In most langues iterating over j seems the logical choice but not in this case. Any ideas? Thank you.

Let's continue from the code sample you have provided.

1) Getting the keys of each element

for (let i in zk) {
  for (let j in zk[i]) {
    console.log(j)
  }
}

2) Getting the list within each element

for (let i in zk) {
  for (let j in zk[i]) {
    console.log(zk[i][j])
  }
}

3) Iterating through each list within each element

for (let i in zk) {
  for (let j in zk[i]) {
    for (let k = 0; k < zk[i][j].length; k++) {
      console.log(zk[i][j][k])
    }
  }
}

Alternatively, you can use Object.values, which returns the values of each key-value pair. You can consider using Array.map() too.

for (let i in zk) {
  Object.values(zk[i]).map(list => {
    list.map(element => {
      console.log(element);
    });
  });
}

Dictionary get value without knowing the key, Dictionaries map keys to values and store them in an array or collection. a double underscore at the beginning of and at the end of the method's name. not iterate through a dictionary in Python if you just want to know if certain key (or value� Iterating over a dictionary can be rather slow when compared with iterating over something like an array. In my tests an iteration over an array took 0.015003 seconds whereas an iteration over a dictionary (with the same number of elements) took 0.0365073 seconds that's 2.4 times as long! Although I have seen much bigger differences.

How to Iterate Through a Dictionary in Python – Real Python, Iterate over the dictionary using for loop over keys | “for in dictionary” Its not an efficient solution because we are iterating over all the keys in dictionary and for each key we are again searching if __name__ == "__main__":. To iterate over (key, value) pairs of a Dictionary, use for loop. You can also access the index of the (key, value) paris using the enumerated Dictionaries. Example to iterate a Dictionary in Swift In the following example, we shall create a dictionary, and print them by iterating over the (key, value) pairs.

You missed the object of which value has to be printed

for (var i in zk) {
    for (var j in zk[i]) {
        console.log(zk[i][j])
    }
}

Different ways to Iterate / Loop over a Dictionary in Python , The code below defines a for loop that iterates over a dictionary named d . For each loop iteration Python automatically assigns the value of key to the name of� Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. There are multiple ways to iterate over a dictionary in Python. Iterate through all keys; Iterate through all values

The issue is that you're missing one level of your loop. You have to loop over the outer array, then loop over the keys in each object, then if that key's value is an array loop over that array.

If for example you want to log all the values then you can use Object.prototype.keys() it returns an array of the keys in an object so you can try something like this:

const arr = [
        {"key1": ["2019-04-12-14:54:29.190", "19", "0", "4325", "1"]},
        {"key2": ["2019-04-12-14:54:29.191", "20", "0", "2212", "1"]},
        {"key3": ["2019-04-12-14:54:29.192", "22", "0", "4376", "0"]}
    ];

    arr.forEach(x => {
      Object.keys(x).forEach(k => {
        if (Array.isArray(x[k])) {
          x[k].forEach(v => {
            console.log(v);
          });
        } else {
          console.log(x[k]);
        }
      });
    });

Not using items() to iterate over a dictionary — Python Anti-Patterns , a set of values. Each insertion to the Dictionary consists of a Key and its associated Value a Dictionary. There are many different ways to iterate over a Dictionary in C# Dictionary is a generic type, Hashtable is not. String to byte Array. The newest methods convert the object into an array and then use array looping methods to iterate over that array. Let us begin with the first method! forin Loop. The simplest and straightforward way to iterate over object properties is by using the forin statement. This method works in all modern and old browsers including Internet

In js you always get the keys so what you do is:

for (var i in zk) {
    for (var j in zk[i]) { // j wil ben key1, key2 ect
        console.log(zk[i][j]); // arrays  ["2019-04-12-14:54:29.190", "19", "0", "4325", "1"] over where you can itterate
        for (var k in zk[i][j]) {
            console.log(zk[i][j][k]); // "2019-04-12-14:54:29.190" ect
        }
    }
}

What is the best way to iterate over a Dictionary in C#?, A Python dictionary is like an associative array in other programming languages. The basic concept behind a Python dictionary is the idea of a key:value pair. Consider You simply refer to each key:value pair using the name of the key. Since dictionary key:value pairs are unordered, how do we iterate over those pairs. The Java iterate through ArrayList programs. Learn how to retrieve values from ArrayList in Java using for loop, while loop, iterator and stream api.. There are five ways to loop ArrayList.

Python Dictionaries, To iterate over (key, value) pairs of a Dictionary, use for loop. If you do not intend to use the index, you may remove the variable name, index, from the for loop or replace it with an Swift - Convert Dictionary into Arrays of Keys and Values. Note: The if condition above is necessary, only if you want to iterate the properties which are dictionary object's very own. Because for..in will iterate through all the inherited enumerable properties. Or. Object.keys(dictionary).forEach(function(key) { console.log(key, dictionary[key]); });

Iterate through (key,value) pairs of Dictionary in Swift, for (var key in dictionary) { // do something with key }. Example: Program to loop through associative array and print keys. filter_none. edit close. Iterating through a dictionary only gives you the keys. You told python to expect a bunch of tuples, and it tried to unpack something that wasn't a tuple (your code is set up to expect each iterated item to be of the form (key,value) , which was not the case (you were simply getting key on each iteration).

How to get a list of associative array keys in JavaScript , The order of the array returned by Object.entries() does not depend obj: The object whose own enumerable string-keyed property [ key The ordering of the properties is the same as that given by looping over the property values of the object manually. displayName � Function.length � Function.name. Then, we add the key:value pair i.e people[3]['Name'] = 'Luna' inside the dictionary 3. Similarly, we do this for key age, sex and married one by one. When we print the people[3], we get key:value pairs of dictionary 3. Example 4: Add another dictionary to the nested dictionary

Comments
  • zk[i][j] is the array ? But you should actually change your datastructure, either use an array of objects or one object with keys for lookup, not both.
  • You can use for..of to iterate through values instead of keys developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • thing.map(Object.values) or thing.flatMap(Object.values) or thing.flatMap(Object.values).flat() depending on how exactly you want it.