5


Im trying to get data in pylon to use in jquery autocomplete, the librarary i'm using for autocomplete it requires this format

abc
pqr
xyz

and in python i have data in this format

[["abc"], ["pqr"],["xyz"]

How do i convert this list to the above one.

Edit:
I trying to use these for a autocompete and i'm using pylons, in which the query to the server return list in this format

    [["abc"], ["pqr"],["xyz"]

http://jquery.bassistance.de/autocomplete/demo/ this library except remote call in

abc 
pqr
xyz

i tried to use

"\n".join(item[0] for item in my_list)

but it returns data in firebug like this.

'asd\ndad\nweq'

i want it to be in

abc
pqr
xyz

any help would be appreciated as i'm a PHP developer this is first time i'm trying to do code in python.

thnaks

m4k
  • 69
  • 1
  • 1
  • 4

5 Answers5

13
"\n".join(item[0] for item in my_list)

However, what's this got to do with JSON...?

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
2

Your code is doing what you want it to, but I imagine you're inspecting the results in the python REPL or ipython, and expecting to see new lines instead of '\n'.

In [1]: items = [["abc"], ["pqr"],["xyz"]]
In [2]: s = "\n".join(item[0] for item in items)
In [3]: s
Out[3]: 'abc\npqr\nxyz'
In [4]: print s
abc
pqr
xyz
Jimothy
  • 9,150
  • 5
  • 30
  • 33
2

I am not exactly sure what you want, but you may try:

nested_list = [ ["abc"], ["pqr"], ["xyz"] ]
data = "\n".join( (item[0] for item in nested_list) )

This will convert your list of lists into a string separated by newline characters.

Jim Brissom
  • 31,821
  • 4
  • 39
  • 33
1

Er I'm not sure what exactly you want, but if you need to print that you could do

for l in data:
    print l[0]

or if you want to make it a flat list, you could do something like

map(lambda x: x[0], a)

or if you even just want a single string with newlines, you could do something like

"\n".join(map(lambda x: x[0], a))

Dunno if that helped at all, but wish you luck

Xzhsh
  • 2,239
  • 2
  • 22
  • 32
0

I think you want this, though it's hard to know based on your description:

  >>> l = [["abc"],["pqr"],["xyz"]]
  >>> "".join(map(lambda a:a[0] + "\n",l))
  'abc\npqr\nxyz\n'
Shlomi Fish
  • 4,380
  • 3
  • 23
  • 27