Checklist of Understanding Lists and Dicts

Accessing a simple list

Start off with the variable mylist set to this list:

mylist = ['oranges', 'pears', 'kiwis', 'apples', 'eggs' ]

Questions

  1. Get the number of elements in the mylist.
  2. Get the first element in the mylist.
  3. Get the last element in the mylist
  4. Get the second element in the mylist
  5. Get the second to last element in the mylist
  6. Make a slice (i.e. sublist) of mylist, beginning with the 3rd element.
  7. Make a slice of mylist, beginning with the 2nd element, through the 2nd-to-last element.

Expected results

  1. Get the number of elements in the mylist.
5
  1. Get the first element in the mylist.
THING
  1. Get the last element in the mylist
THING
  1. Get the second element in the mylist
THING
  1. Get the second to last element in the mylist
THING
  1. Make a slice (i.e. sublist) of mylist, beginning with the 3rd element.
THING
  1. Make a slice of mylist, beginning with the 2nd element, through the 2nd-to-last element.
THING

Solutions

  1. Get the number of elements in the mylist.
>>> len(mylist)
5
  1. Get the first element in the mylist.
>>> mylist[0]
mylist[0]
  1. Get the last element in the mylist
>>> mylist[-1]
THING
  1. Get the second element in the mylist
>>> mylist
THING
  1. Get the second to last element in the mylist
>>> mylist
THING
  1. Make a slice (i.e. sublist) of mylist, beginning with the 3rd element.
>>> mylist
THING
  1. Make a slice of mylist, beginning with the 2nd element, through the 2nd-to-last element.
>>> mylist
THING

Looping through a list

Questions

  1. Print out each element in mylist, but in upper-case.
  2. Count the number of elements in mylist using a for-loop (i.e. without using len)
  3. Get the sum of the lengths of each element (i.e. character length) in mylist

Sorting a list with sorted()

  1. Make a new copy of mylist, except with the elements sorted in alphabetical order.
  2. Make a new copy of mylist, except with the elements sorted in reverse-alphabetical order
  3. Make a copy of mylist with the elements sorted by length, shortest to longest.
  4. Make a copy of mylist with the elements sorted by length, longest to shortest

5. Make a copy of mylist with the elements sorted by length, longest to shortest, and in case of a tie, in alphabetical order. 6.