Question: I am representing a weighted graph as a dictionary where the key represents a vertex and the following lists represent the edges incident on the vertex (first number weight of edge and second number adjacent vertex):

```
wGraph = { 1 : [[2, 2],[3, 4],[3, 3]],
       3 : [[3, 2],[5, 4],[7, 5]],
       2 : [[2, 1],[4, 4],[3, 6]],
       4 : [[3, 1],[4, 2],[1, 6],[5, 3]],
       6 : [[3, 2],[1, 4],[8, 5]],
       5 : [[7, 3],[8, 6],[9, 7]],
       7 : [9, 5]}

```

I would like to save a random key without its value from the dictionary to a list called visited.

```
random_num = random.randrange(len(wGraph))

visited = []

```

How can I get the key from the dictionary according to the random number?

Answer: 
To make a random choice from some values, use `random.choice`. You want to choose from the keys of the dictionary, so that's exactly what you pass in:

```
random.choice(wGraph.keys())

```
```
import random

visited = []

def random_key():
    wGraph = {1: [[2, 2], [3, 4], [3, 3]],
              3: [[3, 2], [5, 4], [7, 5]],
              2: [[2, 1], [4, 4], [3, 6]],
              4: [[3, 1], [4, 2], [1, 6], [5, 3]],
              6: [[3, 2], [1, 4], [8, 5]],
              5: [[7, 3], [8, 6], [9, 7]],
              7: [9, 5]}

    random_key = random.choice(wGraph.keys())

    if random_key not in visited:
        visited.append(random_key)
    print visited

for e in range(7):
    random_key()

```