Skip to main content

list_exercise_solution.py


"""
List exercise : solution
"""
import random
import string
#Create a list of the letter of the alphabet in lower case from a to g
# alphabet=['a','b','c','d','e','f','g']
# or
alphabet=list(string.lowercase[0:7])
print alphabet
#shuffle the list
random.shuffle(alphabet)
print alphabet
#append the letter h
alphabet.append('h')
#sort the list again
alphabet.sort()
print alphabet
#append a random letter from i to n
alphabet.append(random.choice(['i','j','k','l','m','n']))
print alphabet
#remove the first element from the list
alphabet.pop(0)
print alphabet
#what is the first element in your list
print alphabet[0]
#create a new list by concatenating the last three elements of this list with the first two elements of this list
newlist=alphabet[-3:]+alphabet[:2]
print newlist
#what is the 4th element in your new list?
print newlist[3]