In: Computer Science
how to use subsetting in Python lists. Practice making subsets of the namelist.
import matplotlib.pyplot as plot
# set up your lists
numlist = [8, 6, 5, 3]
namelist = ['freshmen', 'sophomores', 'juniors', 'seniors']
colorlist = ['red', 'green', 'pink', 'yellow' ]
explodelist = [0.1, 0.0, 0.0, 0.0]
# make the pie chart
plot.pie(numlist, labels=namelist, autopct='%.2f%%', colors=colorlist,
explode = explodelist, startangle = 90)
plot.axis('equal')
plot.savefig('piechart.png')
ANSWER : HERE IS THE ANSWER FOR YOUR QUESTION:
----------------------------------------------------------------------------------------------------------------
CODE:
import matplotlib.pyplot as plot
# set up your lists
numlist = [8, 6, 5, 3]
namelist = ['freshmen', 'sophomores', 'juniors', 'seniors']
colorlist = ['red', 'green', 'pink', 'yellow' ]
explodelist = [0.1, 0.0, 0.0, 0.0]
# make the pie chart
plot.pie(numlist, labels=namelist, autopct='%.2f%%', colors=colorlist,
explode = explodelist, startangle = 90)
plot.axis('equal')
plot.savefig('piechart.png')
#making subsets of the namelist using slicing operator (:)
print("the main list is " +str(namelist))
print("List with first 2 indeces is :" + str(namelist[:2]))
print("List with first 3 indeces is :" + str(namelist[:3]))
print("List with last 2 indeces is :" + str(namelist[2:]))
----------------------------------------------------------------------------------------------------------------
SNIPPET:
----------------------------------------------------------------------------------------------------------------
OUTPUT:
----------------------------------------------------------------------------------------------------------------
I hope this would help you out.
If you like my answer , please upvote
If you have any doubt, you can provide comment /feedback below the answer
Thanks