-3

So i have a list and a for loop (still a beginner sorry) and i want this code but in a more simplified way and without a new backwards list. ^The edit

lyrics = [["First", "and a partride in a pear tree",], ["second", "2 things"], ["Third", "3 things"], ["Fourth", "4 things"], ["Fifth", "5 things"], ["Sixth", "six things"], ["Seven", "7 things"], ["Eigth", "8 things"], ["Nineth", "nine things"], ["tenth", "Ten things"], ["eleventh", "eleven things"], ["Twelveth", "twelve things"]]

backwards = []

for i in range(12):
    print("On the", lyrics[i][0], "my true love gave to me,               lyrics[i][1])
backwards.append(lyrics[i][1])
for each in backwards:
    print (each) #Forgot how i did it in the reverse order but i want this in a more simplified version to learn from.

PS: Would like as few lines as possible (im able to do it in 8 lines but would like at least 3-4) :/

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Envurb
  • 1
  • 2

2 Answers2

1

First, don't use list for your variables: it's a built-in function.

Second, print slices:

for i in range(len(l)):
    print(list(reversed(l[:i+1])))
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 1
    `print(reversed(l[:i+1]))` may be. – Ahsanul Haque Dec 03 '16 at 18:39
  • Hi ive read up about slices, can you elaborate on them? going to try to use your improvement now. – Envurb Dec 03 '16 at 18:40
  • 1
    `reversed` returns an iterator on python 3: output is ` `. to get a list do `print(l[:i+1][::-1])` – Jean-François Fabre Dec 03 '16 at 18:40
  • @Envurb Here's a good explanation: http://stackoverflow.com/questions/509211/explain-pythons-slice-notation – DYZ Dec 03 '16 at 18:41
  • Right, added list() to the example. – DYZ Dec 03 '16 at 18:42
  • Doesnt seem to work, what i'm trying to do is a challenge i set myself, but since i don't fully python it's a bit tricky. im trying to code the 12 days of christmas song lol. Il try the edited reversed version now. – Envurb Dec 03 '16 at 18:44
  • It printed with the square brackets. the reversed version brings up an error: 'list' object was not callable – Envurb Dec 03 '16 at 18:48
  • Do you use Python 2 or Python 3? To remove brackets, use function `.join()`. – DYZ Dec 03 '16 at 18:49
  • @Envurb, `list object is not callable` is exactly because you ruined the built-in function `list()`. You need to restart your Python development environment and give your list another name. – DYZ Dec 03 '16 at 18:52
  • Ok it worked however, im using a 2d list. Is there a way to print the second index of the index without the square brackets? – Envurb Dec 03 '16 at 18:56
  • @Envurb, This sounds like a different problem to me. – DYZ Dec 03 '16 at 18:57
  • @DYZ Check the post again, ive included my code. Im trying to basically simplify it to 4 or less lines. – Envurb Dec 03 '16 at 19:04
  • @DYZ sorry i went off course. Was wanting a printing function to print everything in the list on that loop index which corresponds to the list. Just like the answer you gave me, but is there a way to do this with a 2d list and only print the second index of that index on a new line? – Envurb Dec 03 '16 at 19:09
  • @Envurb I am still not sure what you want. Give an example of desired output. – DYZ Dec 03 '16 at 19:14
  • @DYZ output: First day) 1 thing \n second day) 2 things 1 thing \n third day) 3 things, 2 things, 1 thing. #Where the 'day' and 'things' are stored in a 2d list – Envurb Dec 03 '16 at 19:20
  • I will create a new question now to avoid the confusion, sorry about all of this. – Envurb Dec 03 '16 at 19:22
  • it wont let me create a new question :/ – Envurb Dec 03 '16 at 19:23
  • Ok i changed the title and the post again to make it less confusing. – Envurb Dec 03 '16 at 19:25
1

You may use range here:

my_str = ['A', 'B', 'C']
for i, val in enumerate(my_str):
    print ' '.join(my_str[i::-1])

OR, in one line as:

print '\n'.join(' '.join(my_str[i::-1]) for i in range(len(my_str))

Both of these will print:

A
B A
C B A

I am not sure whether this is what is desired. This result is based on:

How can i make it so A will be printed and then B and A, and finally C and B and A.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • Yes, but no however. I updated my code on the post. Im using a 2d list. So is there a way to do this with your code to only print the second index of that list index? – Envurb Dec 03 '16 at 19:15
  • 1
    @Envurb: Please mention the input list and the desired output. Right now I do not know what you want. Also, since what you desire now is totally different from what you mention at the first place. Create a separate question and roll back this question to the previous version – Moinuddin Quadri Dec 03 '16 at 19:17
  • that would be a good idea. Sorry about this, i've never really asked for help with my code. Il make a new question now. – Envurb Dec 03 '16 at 19:21
  • it wont let me create a new question :/ – Envurb Dec 03 '16 at 19:23
  • Ok i changed the title and the post again to make it less confusing. – Envurb Dec 03 '16 at 19:25