Merging 2 lists in Python
Question: With my current script

    
    
    from lxml import html
    import requests
    from bs4 import BeautifulSoup
    import re
    import csv
    import itertools
    
    
    r = requests.get("http://www.mediamarkt.be/mcs/productlist/_128-tot-150-cm-51-tot-59-,98952,501091.html?langId=-17")
    
    soup = BeautifulSoup((r.content),'lxml')
    
    links = soup.find_all("h2")
    
    g_data = soup.find_all("div", {"class": "price small"})
    
    for item in g_data:
        prijs =[item.text.encode("utf-8") for item in g_data]
    
    for link in links:
        if "TV" in link.text:
            product = [link.text.encode("utf-8").strip() for link in links if "TV" in link.text]
    
    for item in itertools.chain(prijs + product):
        print item
    

I'm getting a list with first all the "prijs" and below all the "products. for
example: prijs prijs prijs product product product

I would like to get the following result Prijs Product

Prijs Product

Prijs Product

Thank you


Answer: The nature of the problem seems to have little to do with your actual code, so
in order to make this question useful for future readers I am going to give
you a general answer using example lists.

Don't concatenate your two lists. Generate a list of pairs with `zip`, then
flatten the result.

    
    
    >>> lst1 = ['a1', 'a2', 'a3']
    >>> lst2 = ['b1', 'b2', 'b3']
    >>> [x for pair in zip(lst1, lst2) for x in pair]
    ['a1', 'b1', 'a2', 'b2', 'a3', 'b3']
    

The flattening looks a little bit nicer with `itertools.chain`.

    
    
    >>> list(chain.from_iterable(zip(lst1, lst2)))
    ['a1', 'b1', 'a2', 'b2', 'a3', 'b3']
    

Alternatively, with unpacking:

    
    
    >>> list(chain(*zip(lst1, lst2)))
    ['a1', 'b1', 'a2', 'b2', 'a3', 'b3']
    

Since you are using Python 2, all of the above could be made more memory
efficient by using `itertools.izip` instead of `zip` (the former returns an
iterator, the latter a list).

