You're given a string containing a sequence of words separated with whitespaces. Let's say it is a sequence of patterns: a name and a corresponding number - like this:

```"red 1 yellow 2 black 3 white 4"```

You want to turn it into a different **string** of objects you plan to work with later on - like this:

```"[{name : 'red', id : '1'}, {name : 'yellow', id : '2'}, {name : 'black', id : '3'}, {name : 'white', id : '4'}]"```

Doing this manually is a pain. So you've decided to write a short function that would make the computer do the job for you. Keep in mind, the pattern isn't necessarily a word and a number. Consider anything separeted by a whitespace, just don't forget: an array of objects with two elements: name and id.

As a result you'll have a string you may just copy-paste whenever you feel like defining a list of objects - now without the need to put in names, IDs, curly brackets, colon signs, screw up everything, fail searching for a typo and begin anew. This might come in handy with large lists.

Difficulty: introductory

Starter code:

def words_to_object(s):
	

Solutions:
import re

def words_to_object(s):
    return "[" + re.sub("([^ ]+) ([^ ]+)", r"{name : '\1', id : '\2'},", s).strip(',') + "]"

def words_to_object(s):
    return '[' + ', '.join("{name : '%s', id : '%s'}" % (n, i) for n, i in zip(s.split()[::2], s.split()[1::2])) + ']'

def words_to_object(s):
    words = s.split()
    
    res = []
    
    for key in range(0, len(words) - 1, 2):
        value = key + 1
        
        res.append("{name : " + "'" + words[key] + "'" + ", " + "id : " + "'" + words[value] + "'}")
    
    return "[" + ", ".join(res) + "]"

def words_to_object(s):
  return '[' + ', '.join(f"{{name : '{a}', id : '{b}'}}" for a, b in zip(s.split()[::2], s.split()[1::2])) + ']'

def words_to_object(s):
    xs = s.split()
    return '[{}]'.format(', '.join(f"{{name : {name!r}, id : {id_!r}}}" for name, id_ in zip(xs[::2], xs[1::2])))

import collections
import json
import re
def words_to_object(s):
    if s == "" :
        return '[]'
    d = collections.namedtuple('d', 'name ,id')
    words = s.split(" ")
    names = [ x for x in words[::2]]
    ids =  [x for x in words [1::2]]
    result =""
    i = 0
    for i in range(0,int( len(words) /2)):
       a = d(name= names[i], id=ids[i])
       r=(json.dumps(a._asdict()).replace('"', '\''))
       r= r.replace('"','').replace("'name':", "name :").replace("'id':", "id :")
       r= re.sub("}","}, ",r,flags=re.UNICODE)
       result +=r
    return '[' +result[0:-2] +"]"


objectify = "{{name : '{}', id : '{}'}}".format

def words_to_object(s):
    pairs = zip(*[iter(s.split())]*2)
    obj_list = ', '.join(objectify(*pair) for pair in pairs)
    return '[{}]'.format(obj_list)

words_to_object = lambda s: "[" + ", ".join("{name : '%s', id : '%s'}" % t for t in zip(*[iter(s.split())]*2)) + "]"

def words_to_object(s):
    if not s:
        return "[]"
    s = s.split()
    s = [(s[i], s[i+1]) for i in range(0, len(s), 2)]
    s = ["{{name : '{}', id : '{}'}}".format(i[0], i[1]) for i in s]
    return '[' + ', '.join(s) + ']'

import re

def words_to_object(s):
    matches = re.findall(r'(.+?)\s(.+?)\s', s + ' ')
    return "[" + ", ".join(["{{name : '{}', id : '{}'}}".format(x[0], x[1]) for x in matches]) + "]"

def words_to_object(s):
    if s == '': return '[]'
    for iii in range(s.count(' ')):
        s = s.replace(' ',chr(9),1)
        s = s.replace(' ',chr(10),1)
    s_list_a = s.split(chr(10))
    retval_list = []
    for val in s_list_a:
        zname, zid = val.split(chr(9))
        retval_list.append('''{name : '''+chr(39)+zname+chr(39)+''', id : '''+chr(39)+ zid+chr(39))
    retval = '''[''' + '}, '.join(retval_list)+'''}]'''
    return retval

def words_to_object(s):
    s=s.split()
    return str([{'name': s[i], 'id': s[i+1]} for i in range(0,len(s),2)]).replace("'name'","name ").replace("'id'","id ")

def words_to_object(s):
    s = s.split()
    return "["+", ".join(f"{{name : '{s[i]}', id : '{s[i+1]}'}}"  for i in range(0,len(s),2))+"]"

def words_to_object(s):
    return'['+', '.join(f"{{name : '{a}', id : '{b}'}}"for a,b in zip(*[iter(s.split())]*2))+']'

def words_to_object(s):
    nis = s.split()
    dicts_list = [{'name': name, 'id': id} for name, id in zip(nis[::2], nis[1::2])]
    return f"{dicts_list}".replace("'name'", "name ").replace("'id'", "id ")

def words_to_object(s):
    ss = s.split()
    return '[' + ', '.join(['{'+'name : \'{0}\', id : \'{1}\''.format(ss[i*2], ss[i*2+1])+'}' for i in range(len(ss)//2)]) + ']'

def words_to_object(s):
    s = s.split()
    arr = [{'name':i,'id':j} for i,j in zip(s[::2], s[1::2])]
    return str(arr).replace("'name':",'name :').replace("'id':",'id :')

def words_to_object(s):
    a=s.split(' ')
    r=[]
    for name,_id in zip(a[::2],a[1::2]):
        r.append("{{name : '{}', id : '{}'}}".format(name,_id))
    return '[{}]'.format(', '.join(r))

def words_to_object(s):
    if s:
        s=s.split(" ")
        return "["+', '.join("{"+"name : '{}', id : '{}'".format(s[i],s[i+1])+"}" for i in range(0,len(s),2))+"]"
    else:
        return '[]'

def words_to_object(s):
    s = s.split()
    return "[" + ', '.join(["{name : '%s', id : '%s'}"%(i,j) for i,j in zip(s[::2], s[1::2])]) + "]"