1

There is a lot function involved but im gonna keep it simple :

Here is my code :

[['Musique', 'Initiation au tango argentin suivi de la milonga', 182, 231], ['Musique', 'The Singing Pianos', 216, 216], ['Musique', 'Rythmes et détente : Duo Pichenotte', 216, 216]]

I want to return only the index [1] of every sublist as strings. It's in french but the index [1] is the title of every sublists. Every sublists are an event and i need to return only the name. There is actually a lot more events in my code but i want a simple code and ill do my best with it.

So if we're looking at the code sample i gave you i would have to return :

Initiation au tango argentin suivi de la milonga
The Singing Pianos
Rythmes et détente : Duo Pichenotte

If there was a way to return them on different lines like my exemple of return, that would be great too.

What I have tried:

Im having a hard time using index in a list of sublists. It's important to return only the title as str of every lists. i tried using a while like

while i < len(events):

    print(events[i][:1][0:1])  # That would search every index i need, right ?
but it didnt work. there is more code involved but you get the picture and i dont want to add 8 functions to this scenario.
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Oxynor
  • 41
  • 6

1 Answers1

0
l=[['Musique', 'Initiation au tango argentin suivi de la milonga', 182, 231], ['Musique', 'The Singing Pianos', 216, 216], ['Musique', 'Rythmes et détente : Duo Pichenotte', 216, 216]]

Then Try this:

print('\n'.join([i[1] for i in l]))

Or then:

print('\n'.join(list(zip(*l))[1]))

Or then (numpy):

import numpy as np
l2=np.array(l)
print('\n'.join(l2[:,1].tolist()))

All output:

Initiation au tango argentin suivi de la milonga
The Singing Pianos
Rythmes et détente : Duo Pichenotte
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    U9, great answer, i was not familiar of join() yet, that being said, i have a print on the same line and even tho the code is before my loop that you just gave me its returning after the loop, heres the code : print('Il y a', len(evenements), "évènements du type recherché à la date en question. Ils sont :", print('\n'.join([i[1] for i in evenements]))) – Oxynor Oct 22 '18 at 06:58
  • @Oxynor Remove join then it will be a list, more familiar, then you can do anything you want – U13-Forward Oct 22 '18 at 06:59
  • @Oxynor can try: `print('Il y a', len(evenements), "évènements du type recherché à la date en question. Ils sont :", '\n'.join([i[1] for i in evenements])) ` – U13-Forward Oct 22 '18 at 07:01
  • 1
    U9, i had to modify a bit of my code for my assignments and now im only having a list with all the elemtns and no sublists, do you know what do modify in the code you gave me ? Cuz im not familiar with the kind of for loop you gave me – Oxynor Oct 22 '18 at 18:15
  • 1
    Nevermind, solved, thanks ! – Oxynor Oct 22 '18 at 19:04
  • @Oxynor Happy that you solved it :-), hope you continue well :-) – U13-Forward Oct 23 '18 at 00:00