# Tutorials/Lists

Jump to: navigation, search

Prerequisites: The Basics, Datatypes.

# Lists

We have seen simple lists already. Can you remember how to create a list of numbers/symbols/characters? How would you add or join two lists?

```q)a:1 2 3			// defining some lists
q)b:7 8 9
q)symlist:`john`katie`david
q)charlist:"helloworld"

q)a+b				// add
8 10 12
q)c:a,b			        // join
q)c
1 2 3 7 8 9
```

Here are some more operators/functions which you might find useful. See if you can work out what they’re doing. Have a look at the reference page for more information.

```q)first symlist
`john
q)-2#c                          // take
8 9
q)5#a
1 2 3 1 2
q)1_c                           // drop/cut
2 3 7 8 9
```

### Single-element lists

At this point, we should discuss single-element lists. You may have noticed that using the take operator with 1 on the left (`1#`) gives a strange-looking result. That’s what a single-element list looks like.

```q)3#c				// 3-element list
1 2 3
q)2#c				// 2-element list
1 2
q)1#c				// 1-element list
,1
q)type 1#c
7h
q)type first 1#c		// "first" gets an atom
-7h
q)enlist 5			// 1-element list from atom
,5
q),5                            // but not this way!
',
```

As you can see in the example, `enlist` is used to create a list with one element. (The comma in the display distinguishes a 1-element list from an atom.)

### Indexing

What if I want to specify exactly which elements of the list to return? This is called indexing into the list, and in q the indices run from 0 to n-1 for an n-item list.

```q)charlist
"helloworld"
q)charlist           // getting the first element of c
"h"
q)charlist[5 1 2 2]     // indexing with brackets
"well"
q)charlist 0 1 7 9      // indexing without brackets (juxtaposition)
"herd"
```

The square brackets in the syntax are optional.

### Search and comparison

We often need to search through a list for a particular value, or return all of the values that satisfy a condition. `?` is the search/lookup operator, and returns the first index (only) whose value matches the input.

```q)p:12 8 10 1 9 11 5 6 1 5 4 13 9 2 7 0 17 14 9 18
q)p?9		// search for 9 in p
4
q)p=9
00001000000010000010b
```

Comparisons such as `=` return a boolean list. There are three 1s in the resulting list above, corresponding to the elements in `c` which have a value of 9. To get the indices, we can use `where`.

```q)where p=9
4 12 18
q)p where p>10
12 11 13 17 14 18
```

### Mixed lists

We can also have a list where the elements of the list are of different datatypes, or even lists themselves.

```q)mixedlist:("hey";1;`sym;1 2 3)
q)mixedlist
"hey"
1
`sym
1 2 3
q)type mixedlist
0h
```

As you can see, the syntax for creating a mixed list separates each element with a semicolon, and surrounds the list in parentheses. q displays each element on a separate line.

Every mixed list has type 0, but this doesn’t give very much information about the list. Use `each` to apply a function to every element of a list.

```q)type each mixedlist
10 -7 -11 7h
q)count each mixedlist
3 1 1 3
```

When indexing into a mixed list, we can use the same notation as before with one addition: in square-bracket notation, a semicolon “;” allows you to index at depth:

```q)mixedlist 2
`sym
q)mixedlist[0 3;1]
"e"
2
```

# Exercises

1. Create a string of your own name. Find the last letter of that string.

2. Copy the following list into your q process.

```t:`b`i`i`n`o`b`k`h`l`m`m`c`e`f`m`m`e`o`c`o
```
• Find the first place/index the symbol ``o` occurs
• Find all of the indices where the symbol ``o` occurs
• How many elements of `t` are later in the alphabet than the letter j? What are those elements?
Hint: Use the comparison operators (`=`,`>`,`<`,...) just like you would with numbers!

3. Create your own mixed list and check that its type is 0h.

• Create a list `i` of ints, and a list `s` of symbols. Join `i` and `s` to form a new list, `m`.
• Create a nested list `n` (AKA a list of lists), with two elements: `i` and `s`.

4. Type out the following

```mylist:("hello";"world";1 2 3 4 5;101b)
```
• Use `first` to get the first element of each element
• Use indexing to get the first element of each element

Hint: your output should look like this:

```"h"
"w"
1
1b
```