0

I want to make a playlist generator, I have a list containing different songs and I want to generate a random numbered list from it containing only 10 items, and this is the code I tried:

import random

a = ['Bruised and Scarred - Mayday Parade',
     'Miracles in December - EXO',
     'All Too Well - Taylor Swift',
     'Gravity - Sara Bareilles',
     'Perfectly Perfect - Simple Plan',
     'Welcome To The Black Parade - My Chemical Romance',
     'Everything Has Changed - Taylor Swift',
     'Champagne - Taylor Swift',
     'Piece of Your Heart - Mayday Parade',
     'Blame It On The Rain - He Is We',
     'Sad Song - We The Kings',
     'Give It All - He Is We',
     ]

for x in range(1, 11):
    for y in random.sample(a, k=10):
        print(str(x) + y)

but I got this output:

1Bruised and Scarred - Mayday Parade
1All Too Well - Taylor Swift
1Blame It On The Rain - He Is We
1Everything Has Changed - Taylor Swift
1Give It All - He Is We
1Miracles in December - EXO
1Perfectly Perfect - Simple Plan
1Gravity - Sara Bareilles
1Sad Song - We The Kings
1Piece of Your Heart - Mayday Parade
2All Too Well - Taylor Swift
2Bruised and Scarred - Mayday Parade
2Blame It On The Rain - He Is We
2Perfectly Perfect - Simple Plan
2Champagne - Taylor Swift
2Everything Has Changed - Taylor Swift
2Piece of Your Heart - Mayday Parade
2Miracles in December - EXO
2Welcome To The Black Parade - My Chemical Romance
2Give It All - He Is We
3Bruised and Scarred - Mayday Parade
3Gravity - Sara Bareilles
3Welcome To The Black Parade - My Chemical Romance
3All Too Well - Taylor Swift
3Perfectly Perfect - Simple Plan
3Sad Song - We The Kings
3Champagne - Taylor Swift
3Everything Has Changed - Taylor Swift
3Piece of Your Heart - Mayday Parade
3Give It All - He Is We
4Everything Has Changed - Taylor Swift
4Blame It On The Rain - He Is We
4Piece of Your Heart - Mayday Parade
4Gravity - Sara Bareilles
4Bruised and Scarred - Mayday Parade
4Welcome To The Black Parade - My Chemical Romance
4Miracles in December - EXO
4Sad Song - We The Kings
4Give It All - He Is We
4Perfectly Perfect - Simple Plan
5Gravity - Sara Bareilles
5Blame It On The Rain - He Is We
5Perfectly Perfect - Simple Plan
5Champagne - Taylor Swift
5Everything Has Changed - Taylor Swift
5Bruised and Scarred - Mayday Parade
5Welcome To The Black Parade - My Chemical Romance
5All Too Well - Taylor Swift
5Give It All - He Is We
5Miracles in December - EXO
6Bruised and Scarred - Mayday Parade
6Champagne - Taylor Swift
6Everything Has Changed - Taylor Swift
6Miracles in December - EXO
6Welcome To The Black Parade - My Chemical Romance
6Sad Song - We The Kings
6All Too Well - Taylor Swift
6Gravity - Sara Bareilles
6Give It All - He Is We
6Perfectly Perfect - Simple Plan
7Gravity - Sara Bareilles
7Sad Song - We The Kings
7Everything Has Changed - Taylor Swift
7Welcome To The Black Parade - My Chemical Romance
7Piece of Your Heart - Mayday Parade
7Blame It On The Rain - He Is We
7Bruised and Scarred - Mayday Parade
7Give It All - He Is We
7Champagne - Taylor Swift
7All Too Well - Taylor Swift
8Sad Song - We The Kings
8Gravity - Sara Bareilles
8Champagne - Taylor Swift
8Blame It On The Rain - He Is We
8Miracles in December - EXO
8Give It All - He Is We
8Welcome To The Black Parade - My Chemical Romance
8Bruised and Scarred - Mayday Parade
8Piece of Your Heart - Mayday Parade
8Everything Has Changed - Taylor Swift
9Gravity - Sara Bareilles
9Champagne - Taylor Swift
9Bruised and Scarred - Mayday Parade
9Blame It On The Rain - He Is We
9Piece of Your Heart - Mayday Parade
9Everything Has Changed - Taylor Swift
9Sad Song - We The Kings
9Welcome To The Black Parade - My Chemical Romance
9All Too Well - Taylor Swift
9Perfectly Perfect - Simple Plan
10Champagne - Taylor Swift
10Blame It On The Rain - He Is We
10Perfectly Perfect - Simple Plan
10Miracles in December - EXO
10Give It All - He Is We
10Piece of Your Heart - Mayday Parade
10Everything Has Changed - Taylor Swift
10Welcome To The Black Parade - My Chemical Romance
10Bruised and Scarred - Mayday Parade
10Sad Song - We The Kings

I want it to contain only 10 items, but I got way too many, could please help me (Sorry for my bad english)

nanashi
  • 43
  • 5

2 Answers2

4

You're getting that output because you have an outer loop that iterates ten times, and an inner loop that also iterates ten times. So you get ten 1s, then ten 2s, etc.

You don't need the outer range() loop. Iterate only over the song choices, using enumerate() to get both the item position and the song title:

for position, title in enumerate(random.sample(a, k=10)):
    print(str(position) + title)
John Gordon
  • 29,573
  • 7
  • 33
  • 58
2

Just remove your outer loop.

The nested loops will give you 10 x 10 lines of output.

Your call to random.sample() already produces the 10 (random) lines of output you need.

Also, use enumerate to iterate through & obtain the song index, starting at 1.

Try this:

import random

a = [
    'Bruised and Scarred - Mayday Parade',
    'Miracles in December - EXO',
    'All Too Well - Taylor Swift',
    'Gravity - Sara Bareilles',
    'Perfectly Perfect - Simple Plan',
    'Welcome To The Black Parade - My Chemical Romance',
    'Everything Has Changed - Taylor Swift',
    'Champagne - Taylor Swift',
    'Piece of Your Heart - Mayday Parade',
    'Blame It On The Rain - He Is We',
    'Sad Song - We The Kings',
    'Give It All - He Is We',
]

for i, y in enumerate(random.sample(a, k=10), 1):
    print(i, y)
costaparas
  • 5,047
  • 11
  • 16
  • 26