tags:

views:

74

answers:

4

This is probably a really silly question but, given the example code at the bottom, how would I get a single list that retain the tuples?

(I've looked at itertools but it flattens everything.)

What I currently get is:

('id', 20, 'integer')
('companyname', 50, 'text')
[('focus', 30, 'text'), ('fiesta', 30, 'text'), ('mondeo', 30, 'text'), ('puma', 30, 'text')]
('contact', 50, 'text')
('email', 50, 'text')

Instead, I need a single level list:

('id', 20, 'integer')
('companyname', 50, 'text')
('focus', 30, 'text')
('fiesta', 30, 'text')
('mondeo', 30, 'text')
('puma', 30, 'text')

('contact', 50, 'text')
('email', 50, 'text')

Code:

def getproducts():
    temp_list = []

    product_list = ['focus', 'fiesta', 'mondeo', 'puma']
    # usually this would come from a db

    for p in product_list:
        temp_list.append((p, 30, 'text'))
    return temp_list

def createlist():    
    column_title_list = (
       ("id", 20, "integer"),
       ("companyname", 50, "text"),
       getproducts(), 
       ("contact", 50, "text"),
       ("email", 50, "text"),
    ) 
    return column_title_list

for item in createlist():
    print item
A: 
def createlist():    
    column_title_list = [ ("id",20,"integer"),
                          ("companyname",50,"text") ]
    column_title_list.extend(getproducts())
    column_title_list.extend( [ ("contact",50,"text"),
                                ("email",50,"text") ] )

    return column_title_list
Ofri Raviv
Hi, see below..
alj
+2  A: 

Can you make it into

[[("id",20,"integer")],
 [("companyname",50,"text")],
 getproducts(),
 ...]

? If so, you just need to concatenate the lists.

return sum(column_title_list, [])

You could also use

return [("id",20,"integer"),("companyname",50,"text")] + getproducts() + ...
KennyTM
Hi KennyTM and Ofri. Both your answers are very similar. Thanks. I'll have a look at doing it this way.
alj
A: 

Try working with a list instead of a tuple. You can turn a list into a tuple, when you finished assembling it.

#!/usr/bin/env python

def getproducts():
    temp_list=[]
    # usually this would come from a db
    product_list=['focus','fiesta','mondeo','puma'] 
    for p in product_list:
        temp_list.append((p, 30, 'text'))
    return temp_list

def createlist():    
    column_title_list = [
        ("id", 20, "integer"),
        ("companyname", 50, "text")
    ]
    column_title_list += getproducts()
    column_title_list += [
        ("contact", 50, "text"),
        ("email", 50, "text"),
    ]

    return tuple(column_title_list)

for item in createlist():
    print item

This will result in:

# ('id', 20, 'integer')
# ('companyname', 50, 'text')
# ('focus', 30, 'text')
# ('fiesta', 30, 'text')
# ('mondeo', 30, 'text')
# ('puma', 30, 'text')
# ('contact', 50, 'text')
# ('email', 50, 'text')
The MYYN
+2  A: 

This probably isn't the answer you're looking for, but why waste time trying to find a fancy way to solve this when a straight-forward solution lets you move on and solve more interesting parts of your program?

def createlist(): 
    tmp = []   
    tmp.extend([("id",20,"integer"), ("companyname",50,"text")])
    tmp.extend(getproducts())
    tmp.extend([("contact",50,"text"), ("email",50,"text")])
    return tuple(tmp)
Bryan Oakley
thanks bryan. Wise words. I always get stuck trying to come up with something clever. That why it takes me soooo long.
alj