-2
mylist = ['a','b','c','d']

How can I index the list so that I choose only two of the four elements? mylist[] throws all sorts of errors depending what datatype I put in the square brackets for indexing

develarist
  • 1,224
  • 1
  • 13
  • 34

1 Answers1

1

You can use random.sample Try this:

import random
mylist = ['a','b','c','d']

print(random.sample(mylist, 2))
The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24