Extending VirtualBoxes Virtual hard Drive

I usually have one Windows available in hand in a VM. Knowing windows, it would eat all the hard drive you dedicate to it. So at the setup time I decided to give it a flexible size but maximum of 25GB, which seemed fair for Windows 7. But now that I have had it for a while it got quite big and when I wanted to install a new application, I was running out of space.

Extending an existing drive is not possible, but the whole process to achieve what’s needed is very very simple. All you need to is:

  1. Go to VM’s setting / storage and create a new bigger storage, which would be your only HDD
  2. clone the hard drive from the old one to the new one. Here is the VirtualBox’s command:
    $VBoxManage  clonehd /Path/to/old/Windows7.vdi  /path/to/new/Win7_30GB.vdi --existing
  3. Now remove the HDD from your existing VM. (VM/Setting/Storage)
  4. Add your new HDD to the VM.
  5. Boot the VM
  6. In the guest OS, go to “Control Panel/ Administrative Tools/ Computer Management/ Storage”   and you should be able to see the extra space as unallocated. Just extend the existing drive to the maximum to use it

Enjoy your extended HDD.

Merging Python List of Dictionaries based on specific key

This post is completely based on a cool post on StackOverflow.

So this is the situation, you have two list which they have dictionaries in and you want to merge the dictionaries if they have the same value for one specific field.

x = [{'id':2 , 'name': 'majid'} , {'id':3 , 'name':'maral'}]
y = [{'id':2 , 'num': 22} , {'id':3 , 'num': 33}]

And you want to get the final result like this :

[{'num': 22, 'id': 2, 'name': 'majid'}, {'num': 33, 'id': 3, 'name': 'maral'}]

The function which does the job is as follow (Written by Adam in StackOverflow) :

def merge_lists(l1, l2, key):
  merged = {}
  for item in l1+l2:
    if item[key] in merged:
      merged[item[key]] = item
  return [val for (_, val) in merged.items()]

Now you can test it easily :

merge_lists(x, y , 'id')

Python itertools; Cyclling through a list


If you want to loop through a list in Python, and everytime you hit the last one you loop back and start from the beginning without throwing an exception you can use the Cycle module:

>>> <strong>from itertools import cycle</strong>
>>> my_list = [1,2,3]
>>> <strong>my_cycle = cycle(my_list)</strong>
>>> my_cycle.next()
>>> my_cycle.next()
>>> my_cycle.next()
>>> my_cycle.next()
>>> my_cycle.next()
>>> my_cycle.next()
>>> my_cycle.next()