Data Structures

List

  • Lists in Python represent ordered sequences of values.

  • Lists are "mutable", meaning they can be modified "in place".

# list of integer type
primes = [2, 3, 5, 7]

# list of string type
planets = 
    ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']

# list of lists
hands = [['J', 'Q', 'K'], ['2', '2', '2'], ['6', 'A', 'K']]

# list of different types
my_favourite_things = [32, 'raindrops on roses', help]

Slicing

planets[0:3]    #['Mercury', 'Venus', 'Earth']
planets[:3]     #['Mercury', 'Venus', 'Earth']
planets[3:]     #['Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
planets[1:-1]   #['Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus']
planets[-3:]    #['Saturn', 'Uranus', 'Neptune']

Tuple

Set

Dict

Collections

namedtuple

  • Immutable

deque

Counter

OrderedDict

defaultdict

Last updated