3

This is a function that allows me to print my two lists, cursosArreglo and tareasArreglo, but when I print them they come out like this: [['qwert'], ['fisica']] Esta es la lista de tareas y sus prioridades:

[['zxcvb'], ['4'], ['nbvcx'], ['3'], ['tregd'], ['2'], ['bvxx'], ['3']]

how can i print both lists without the brackets?

def verTareas():
    print("")
    print("Saludos "+nombre+(" a continuacion puede ver su informacion"))
    print("")
    print("Esta es la lista de cursos asignados: ")
    print str(cursosArreglo)
    print("Esta es la lista de tareas y sus prioridades: ")
    print '\n'+str(tareasArreglo)
    print("")
    regresar()
Saksham Varma
  • 2,122
  • 13
  • 15
  • 1
    See [`str.join`](https://docs.python.org/2/library/string.html); possibly use in conjunction with a generator expression or flat map here. – user2864740 Apr 08 '15 at 01:42
  • @user2864740 I tried that, but I got the following error: descriptor 'join' requires a 'str' object but received a 'list' – ProgrammingNewbie Apr 08 '15 at 01:50
  • @luispe str.join works in a sequence of str[ings], you have a sequence of lists. First convert it to a sequence of strings. (I think that the code that creates the list may be accidentally creating the inner lists of one element - they don't appear useful, so perhaps remove them at the source?) – user2864740 Apr 08 '15 at 02:06

4 Answers4

6

For a list of lists, lst just use join with a suitable delimiter, after flattening it. You can use chain of the itertools module to flatten the list:

from itertools import chain

lst = [['zxcvb'], ['4'], ['nbvcx'], ['3'], ['tregd'], ['2'], ['bvxx'], ['3']]
flat_lst =  list(chain.from_iterable(lst))
print ','.join(flat_lst)
Saksham Varma
  • 2,122
  • 13
  • 15
2

Here's some code that works without any modules or anything.

for i in range(len(cursosArreglo)):
    cursosArreglo[i] = "".join(cursosArreglo[i])

print(", ".join(cursosArreglo))

You can also run this for tareasArreglo by substituting it in for cursosArreglo.

This first goes through your list of lists, then substitutes each spot in the list with a string version of it, by using str.join(), then str.join again to fully join the list.

This can also be compressed into one line.

strCursosArreglo = ", ".join("".join(i) for i in cursosArreglo)
michaelpri
  • 3,521
  • 4
  • 30
  • 46
  • Could this be further reduced by iterating over the string `cursosArreglo` itself instead of its length? By this I mean `for i in cursosArreglo` instead of `for i in range(len(cursosArreglo))`. – Matthew0898 Apr 08 '15 at 02:10
  • @Matthew0898 It could, but then you would have to use `.index`. I prefer it like this – michaelpri Apr 08 '15 at 02:11
  • 2
    Or you can use enumerate(), `for i, s in enumerate(cursosArreglo):` though I don't see why you would assign back into the list. You can definitely iterate over list without an index in the comprehension (generator is better) `", ".join("".join(s) for s in cursosArreglo)` – AChampion Apr 08 '15 at 02:22
  • @achampion That is a very good idea. Would this be considered list comprehension or a generator expression? – Matthew0898 Apr 08 '15 at 02:54
  • 1
    removing the `[]` makes it a generator, i.e. it doesn't populate a whole list and delays execution until needed. – AChampion Apr 08 '15 at 02:58
2

You also use lambda, the below code:

>>> func= lambda lst: [m for i in lst for m in func(i)] if type(lst) is list else [lst]
>>> lst = [['zxcvb'], ['4'], ['nbvcx'], ['3'], ['tregd'], ['2'], ['bvxx'], ['3']]
>>> func(lst)
['zxcvb', '4', 'nbvcx', '3', 'tregd', '2', 'bvxx', '3']

Or,

>>> func=lambda L: sum(map(func, L), []) if isinstance(L, list) else [L]
>>> func(lst)
['zxcvb', '4', 'nbvcx', '3', 'tregd', '2', 'bvxx', '3']
thinkerou
  • 1,781
  • 5
  • 17
  • 28
0

You will need to flatten the list and then join it, e.g. using nested joins:

>>> data = [['zxcvb'], ['4'], ['nbvcx'], ['3'], ['tregd'], ['2'], ['bvxx'], ['3']]
>>> print ", ".join("".join(s) for s in data)
zxcvb, 4, nbvcx, 3, tregd, 2, bvxx, 3
AChampion
  • 29,683
  • 4
  • 59
  • 75