dictionary flatten function and extracting