?how to make a two dimensional matrix
<p>I have 1 dictionary and 1 tuple </p>

<pre><code> students = { 0: "Nynke", 1: "Lolle", 2: "Jikke", 3: "Popke", 4: "Teake", 5:
"Lieuwe", 6: "Tsjabbe", 7: "Klaske", 8: "Ypke", 9: "Lobke"}

friendships = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4), (4, 5), (5,
6), (5, 7), (6, 8), (7, 8), (8, 9)]
</code></pre>

<p>First thing I have to make is a 2-dimensional matrix wherein for each student a list with their friends like:  [[1, 2], [0, 2, 3], ..., [8]]</p>

<p>the second thing is i have to sort the list based on the number of friends i have to print it like: [(9, 1), (0, 2), ..., (8, 3)]. 
number 9 has only 1 friend, number 0 has two friends etc...</p>

<p>my code:</p>

<pre><code>for i in students:
    for x in friendships:
        if students[i] == friendships(x):
            new_list.append(x)
    print(i)
</code></pre>

<p>Not a 100% sure I understand exactly what you're trying to do, so correct me if I have the wrong idea. But here's what I think,</p>

<p>For the first one you can try creating a dictionary to store every friendships</p>

<pre><code>all_friends = {}
for f in friendships:
   if f[0] in all_friends:
      all_friends[f[0]] = all_friends[f[0]] + [f[1]]
   else:
      all_friends[f[0]] = [f[1]]
</code></pre>

<p>Then you'll want to get a list of this dictionaries values, and that'll be your 2d matrix:</p>

<pre><code>all_friends.values()
</code></pre>

<p>For the second one you will first need to keep track of how many friendships each person has, try a dictionary for this</p>

<pre><code>friendships_dict = {}
for f in friendships:
   if f[0] in friendships_dict{}:
      friendships_dict[f[0]] = friendships_dict[f[0]] + 1
   else:
      friendships_dict[f[0]] = 1
</code></pre>

<p>Then you'll want to make that a list of tuples that you can sort:</p>

<pre><code>friends_list = [(f1, f2) for f1, f2 in friendships_dict.iteritems()]

sorted_friends_list = sorted(friends_list, key=lambda x: x[1])
</code></pre>

<p><strong>EDIT</strong></p>

<p>Now that I better understand the first part of your question, let me go ahead and try to offer a solution.</p>

<p>You'll still want the first piece of code I included: </p>

<pre><code>all_friends = {}
for f in friendships:
   if f[0] in all_friends:
      all_friends[f[0]] = all_friends[f[0]] + [f[1]]
   else:
      all_friends[f[0]] = [f[1]]
</code></pre>

<p>Then add the following: </p>

<pre><code>matrix = [] 
for key in sorted(all_friends.iterkeys()):
    matrix.append(all_friends[key])
</code></pre>

<p>And <code>matrix</code> will be what you wanted </p>

python