Extract three letter pattern from a given list The code takes a list of names and extracts the three letter pattern from the names. Then return the list of names as a value in the dictionary with key as a pattern.  The list name has a fixed naming pattern like three letter Spp in the example. It can extent like App, Bpp, Cpp etc..

How can I improve this code?

import sys
import re

def uniquelist(scaffold_names_list):
    unique_names = []
    for name in scaffold_names_list:
        pattern_name = re.search(r"[A-Z]{1}[a-z]{2}", name)
        unique_names.append(pattern_name.group())
    unique_names = list(set(unique_names))
    return unique_names

def uniquepattern(scaffold_names_list):
    unique_names = uniquelist(scaffold_names_list)
    dct = {}

    for singlepattern in unique_names:
        dct['%s' % singlepattern] = []

    for singelelist in scaffold_names_list:
            number = re.search(r"[A-Z]{1}[a-z]{2}", singelelist)
            number = number.group()

            for singlepattern in unique_names:
                if number == singlepattern:
                    dct[singlepattern].append(singelelist)

    return dct


scaffold_names_list = ['Spp1Aa1', 'Spp1Aa1', 'Spp2Aa1', 'Spp3Aa1', 'Spp4Aa1', 'Spp5Aa1']
patterns = uniquepattern(scaffold_names_list)
print(patterns)

{'Spp': ['Spp1Aa1', 'Spp1Aa1', 'Spp2Aa1', 'Spp3Aa1', 'Spp4Aa1', 'Spp5Aa1']}
Naming

I have two problems with your naming: 

First, you don't use underscores to separate words. Instead of uniquepattern, use unique_pattern. 

Second, your names don't actually tell me anything! Functions, particularly non-method functions, should include a verb unless the verb is implicitly get or is. And implicit is needs to be used with some caution.

So rather than unique_pattern, the name should be some phrase reflective of what your function does. Since you mention "scaffold names", perhaps that function should be group_scaffold_names_by_unique_pattern?

Or maybe the unique pattern has another name? Like prefix? (See my question in the comments.) So perhaps it should be group_scaffold_names_by_prefix?

Is that long? Yes. Does it says what happens? Also, yes. Clarity for the win!

Redundancy. Repetition. Duplication. Multiple Copies.

In coding, if you find yourself typing something for the second time, STOP! Whatever this is needs to be stored once and referenced from different places. Write a function. Store text in a variable. Create a class. Do whatever it takes, but DON'T REPEAT YOURSELF. (This is called the "DRY" principle.)

This code is repeated:

    pattern_name = re.search(r"[A-Z]{1}[a-z]{2}", name)


What does it do? Well, it appears to be used to extract the first matching pattern as the "key" for each name in the list.

So let's write a function for this:

def extract_key(name):
    """ Extract unique pattern from a scaffold name. 
        Apparently, patterns are just the first three characters,
        so don't use regexes, just grab the substring.
    """        
    return name[:3]


Use the standard library

You have a separate function that pulls out the keys (unique patterns) from the names, and returns a uniquified list of them, which you apparently use only to initialize the keys of a dictionary.

Instead of doing that, just use collections.defaultdict to create a dictionary that starts with empty lists:

import collections

groups = collections.defaultdict(list)


With that code, you can be sure that groups[key] is a list (unless you explicitly override it with some non-list value). So let's write your function again:

import collections
from typing import Dictionary, Iterable, List

def group_scaffold_names_by_prefix(names: Iterable[str]) -&gt; Dictionary[str, List[str]]:
    """ Given a list of scaffold names, return a dictionary that
        groups them together by their prefix.
    """
    groups = collections.defaultdict(list)

    for name in names:
        groups[extract_key(name)].append(name)

    return groups