Insert at first position of a list in Python

ID : 20421

viewed : 24

Tags : pythonlistinsertpython

Top 5 Answer for Insert at first position of a list in Python

vote vote


Use insert:

In [1]: ls = [1,2,3]  In [2]: ls.insert(0, "new")  In [3]: ls Out[3]: ['new', 1, 2, 3] 
vote vote


From the documentation:

list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a),x) is equivalent to a.append(x)

vote vote


+ (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize {     //If scaleFactor is not touched, no scaling will occur           CGFloat scaleFactor = 1.0;      //Deciding which factor to use to scale the image (factor = targetSize / imageSize)     if (image.size.width > targetSize.width || image.size.height > targetSize.height)         if (!((scaleFactor = (targetSize.width / image.size.width)) > (targetSize.height / image.size.height))) //scale to fit width, or             scaleFactor = targetSize.height / image.size.height; // scale to fit heigth.      UIGraphicsBeginImageContext(targetSize);       //Creating the rect where the scaled image is drawn in     CGRect rect = CGRectMake((targetSize.width - image.size.width * scaleFactor) / 2,                              (targetSize.height -  image.size.height * scaleFactor) / 2,                              image.size.width * scaleFactor, image.size.height * scaleFactor);      //Draw the image into the rect     [image drawInRect:rect];      //Saving the image, ending image context     UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();      return scaledImage; } 

I propose this one. Isn't she a beauty? ;)

vote vote


There's a great piece of code related to the resizing of images + several other operations. I came around this one when trying to figure ou how to resize images...

vote vote


Here you go. This one is perfect ;-)

EDIT: see below comment - "Does not work with certain images, fails with: CGContextSetInterpolationQuality: invalid context 0x0 error"

// Resizes the image according to the given content mode, taking into account the image's orientation - (UIImage *)resizedImageWithContentMode:(UIViewContentMode)contentMode imageToScale:(UIImage*)imageToScale bounds:(CGSize)bounds interpolationQuality:(CGInterpolationQuality)quality {     //Get the size we want to scale it to     CGFloat horizontalRatio = bounds.width / imageToScale.size.width;     CGFloat verticalRatio = bounds.height / imageToScale.size.height;     CGFloat ratio;      switch (contentMode) {         case UIViewContentModeScaleAspectFill:             ratio = MAX(horizontalRatio, verticalRatio);             break;          case UIViewContentModeScaleAspectFit:             ratio = MIN(horizontalRatio, verticalRatio);             break;          default:             [NSException raise:NSInvalidArgumentException format:@"Unsupported content mode: %d", contentMode];     }      //...and here it is     CGSize newSize = CGSizeMake(imageToScale.size.width * ratio, imageToScale.size.height * ratio);       //start scaling it     CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));     CGImageRef imageRef = imageToScale.CGImage;     CGContextRef bitmap = CGBitmapContextCreate(NULL,                                                 newRect.size.width,                                                 newRect.size.height,                                                 CGImageGetBitsPerComponent(imageRef),                                                 0,                                                 CGImageGetColorSpace(imageRef),                                                 CGImageGetBitmapInfo(imageRef));      CGContextSetInterpolationQuality(bitmap, quality);      // Draw into the context; this scales the image     CGContextDrawImage(bitmap, newRect, imageRef);      // Get the resized image from the context and a UIImage     CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);     UIImage *newImage = [UIImage imageWithCGImage:newImageRef];      // Clean up     CGContextRelease(bitmap);     CGImageRelease(newImageRef);      return newImage; } 

Top 3 video Explaining Insert at first position of a list in Python