# First day

**URL:** https://forum.kx.com/t/first-day/12073
**Category:** Community Support
**Tags:** kdb-and-q
**Created:** [October 12, 2020, 3:55pm UTC](https://forum.kx.com/t/first-day/12073 "2020-10-12T15:55:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![eletrofone1](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@eletrofone1](https://forum.kx.com/u/eletrofone1)
#### Post date: [October 12, 2020, 3:55pm UTC](https://forum.kx.com/t/first-day/12073/1 "2020-10-12T15:55:00Z")

</div>

Good Morning!

I just got to know kdb + and the q language.

I am reading and practicing based on the website [https://code.kx.com/q4m3/](https://code.kx.com/q4m3/).

I just made several examples with lists.

Now I’m in the dictionaries.

Example:

d1: `a`b`c! (1 2; 3 4 5; 6 7 8 9)

Doubt:

How to know if the dictionary contains a certain value?

Ex.: d1 contains 5?

I thank the attention.

---

<div class="post-metadata">

### Author: ![Connor\_Gervin1](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@Connor\_Gervin1](https://forum.kx.com/u/Connor_Gervin1)
#### Post date: [October 12, 2020, 4:11pm UTC](https://forum.kx.com/t/first-day/12073/2 "2020-10-12T16:11:00Z")

</div>

Hi Geraldo,

One solution using each-right syntax (/:)

> q)d1:`a`b`c! (1 2; 3 4 5; 6 7 8 9)<br></font><font face='"monospace"'>q)where 5 in/:d1<br></font><font face='"monospace"'>,`b

For simpler dictionary structures (where the values are atomic) we can use a reverse lookup:

> q)d1:`a`b`c! (1;2;3)<br></font><font face='"monospace"'>q)d1?2<br></font><font face='"monospace"'>`b

Thanks

Connor&nbsp;

![""]()?

---

<div class="post-metadata">

### Author: ![User7](https://avatars.discourse-cdn.com/v4/letter/u/7ea924/32.png) [@User7](https://forum.kx.com/u/User7)
#### Post date: [October 12, 2020, 4:28pm UTC](https://forum.kx.com/t/first-day/12073/3 "2020-10-12T16:28:00Z")

</div>

Hi!

When looking up if a simple dictionary includes a certain value you can use a technique known as Reverse Lookup using the ? (find) operator. See example 1. This will return the key that particular value is stored under.&nbsp;  
Ex 1:

q)d: 1 2 3 4!5 6 7 8

q)d

1| 5

2| 6

3| 7

4| 8

q)d?5

1

q)d?6

2

When using Reverse Lookup in a non-simple dictionary the find (?) operator won’t work without the entire list of values for a particular key. See example 2.&nbsp;

Ex 2:

q)d:(`a`b;&nbsp;`c`d`e;&nbsp;enlist&nbsp;`f)!10&nbsp;20&nbsp;30&nbsp;

q)d?20&nbsp;

`c`d`e&nbsp;

q)d:`a`b`c!(10&nbsp;20;&nbsp;30&nbsp;40&nbsp;50;&nbsp;enlist&nbsp;60)&nbsp;

q)d&nbsp;`b&nbsp;

30&nbsp;40&nbsp;50&nbsp;

q)d?30&nbsp;40&nbsp;50&nbsp;

`b&nbsp;

q)d?40

`

In a nested dictionary we can use a mixture of the in operator and the each both iterator to determine the keys and values that contain a boolean true. See example 3.&nbsp;

Ex 3:

q)5 in’d

1| 0

2| 1

3| 0

4| 1

q)d where 1=5 in’d

4 5 6

5 6 7

Thanks.

Caitlin&nbsp;

---

<div class="post-metadata">

### Author: ![eletrofone1](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@eletrofone1](https://forum.kx.com/u/eletrofone1)
#### Post date: [October 12, 2020, 4:29pm UTC](https://forum.kx.com/t/first-day/12073/4 "2020-10-12T16:29:00Z")

</div>

Thank you Connor!

For simple dictionaries I had succeeded.

But I thought of a slightly more “advanced” dictionary.

I had tried to use where and each, but it went wrong.

As today is my first day, I still didn’t know /: (each right)

Fantastic!

I’m already practicing these iterators.

Now I will try to change the value found.

Eg: change from 5 to 8.

A big hug!

Thank you!

Em segunda-feira, 12 de outubro de 2020 às 12:14:44 UTC-3, [cge...@kx.com](mailto:cge...@kx.com) escreveu:

> Hi Geraldo,
> 
> One solution using each-right syntax (/:)
> 
> > q)d1:`a`b`c! (1 2; 3 4 5; 6 7 8 9)<br></font><font face='"monospace"'>q)where 5 in/:d1<br></font><font face='"monospace"'>,`b
> 
> For simpler dictionary structures (where the values are atomic) we can use a reverse lookup:
> 
> > q)d1:`a`b`c! (1;2;3)<br></font><font face='"monospace"'>q)d1?2<br></font><font face='"monospace"'>`b
> 
> Thanks
> 
> Connor&nbsp;
> 
> ![""]()?

---

<div class="post-metadata">

### Author: ![eletrofone1](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@eletrofone1](https://forum.kx.com/u/eletrofone1)
#### Post date: [October 12, 2020, 8:49pm UTC](https://forum.kx.com/t/first-day/12073/5 "2020-10-12T20:49:00Z")

</div>

Caitlin, thank you very much!

As I said, today is my first day with kdb + e q. Many news.

With your explanation and Connor’s explanation, I started to understand better how the q language works.

A big news for me:

q code is has no order of precedence, it is interpreted from right to left.

Note:

q) 5 in`d

q) d where 1 = 5 in’d

presented error.

Look how it worked for me:

q) 5 in /: d

q) where 5 in: / d

q) d where 5 in: / d

Thank you very much for the tips and suggestions.

A big hug to everyone!

Em segunda-feira, 12 de outubro de 2020 às 12:40:28 UTC-3, [caitlin...@aquaq.co.uk](mailto:caitlin...@aquaq.co.uk) escreveu:

> Hi!
> 
> When looking up if a simple dictionary includes a certain value you can use a technique known as Reverse Lookup using the ? (find) operator. See example 1. This will return the key that particular value is stored under.&nbsp;  
> Ex 1:
> 
> q)d: 1 2 3 4!5 6 7 8
> 
> q)d
> 
> 1| 5
> 
> 2| 6
> 
> 3| 7
> 
> 4| 8
> 
> q)d?5
> 
> 1
> 
> q)d?6
> 
> 2
> 
> When using Reverse Lookup in a non-simple dictionary the find (?) operator won’t work without the entire list of values for a particular key. See example 2.&nbsp;
> 
> Ex 2:
> 
> q)d:(`a`b;&nbsp;`c`d`e;&nbsp;enlist&nbsp;`f)!10&nbsp;20&nbsp;30&nbsp;
> 
> q)d?20&nbsp;
> 
> `c`d`e&nbsp;
> 
> q)d:`a`b`c!(10&nbsp;20;&nbsp;30&nbsp;40&nbsp;50;&nbsp;enlist&nbsp;60)&nbsp;
> 
> q)d&nbsp;`b&nbsp;
> 
> 30&nbsp;40&nbsp;50&nbsp;
> 
> q)d?30&nbsp;40&nbsp;50&nbsp;
> 
> `b&nbsp;
> 
> q)d?40
> 
> `
> 
> In a nested dictionary we can use a mixture of the in operator and the each both iterator to determine the keys and values that contain a boolean true. See example 3.&nbsp;
> 
> Ex 3:
> 
> q)5 in’d
> 
> 1| 0
> 
> 2| 1
> 
> 3| 0
> 
> 4| 1
> 
> q)d where 1=5 in’d
> 
> 4 5 6
> 
> 5 6 7
> 
> Thanks.
> 
> Caitlin&nbsp;

---

<div class="post-metadata">

### Author: ![SJT1](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@SJT1](https://forum.kx.com/u/SJT1)
#### Post date: [October 13, 2020, 7:37am UTC](https://forum.kx.com/t/first-day/12073/6 "2020-10-13T07:37:00Z")

</div>

Hi Geraldo

We know code.kx.com is a key resource for solo students of q. For example, your insight about precedence appears at

https://code.kx.com/q/basics/application/#long-right-scope

Much could and should be done to improve the site as a resource for solo study. However, when people study alone it is hard for us to see where difficulties need removing.

Any comments or observations as you learn would be most welcome! (This goes for everyone in this group.)

Best

Stephen

librarian@kx.com

Stephen Taylor | Librarian | **Kx** | +44 7713 400852 | stephen@kx.com

My job is to put information where people can find it.
