Codecademy Logo

Python Collections with AI

Related learning

  • Learn Python programming with AI tools. This course takes you from fundamentals to best practices using Codex CLI for serious development.
    • Includes 4 Courses
    • With Certificate
    • Beginner Friendly.
      7 hours

Python dictionaries

A python dictionary is an unordered collection of items. It contains data as a set of key: value pairs.

my_dictionary = {1: "L.A. Lakers", 2: "Houston Rockets"}

Dictionary Key-Value Methods

When trying to look at the information in a Python dictionary, there are multiple methods that return objects that contain the dictionary keys and values.

  • .keys() returns the keys through a dict_keys object.
  • .values() returns the values through a dict_values object.
  • .items() returns both the keys and values through a dict_items object.
ex_dict = {"a": "anteater", "b": "bumblebee", "c": "cheetah"}
ex_dict.keys()
# dict_keys(["a","b","c"])
ex_dict.values()
# dict_values(["anteater", "bumblebee", "cheetah"])
ex_dict.items()
# dict_items([("a","anteater"),("b","bumblebee"),("c","cheetah")])

Lists

In Python, lists are ordered collections of items that allow for easy use of a set of data.

List values are placed in between square brackets [ ], separated by commas. It is good practice to put a space between the comma and the next value. The values in a list do not need to be unique (the same value can be repeated).

Empty lists do not contain any values within the square brackets.

primes = [2, 3, 5, 7, 11]
print(primes)
empty_list = []

Zero-Indexing

In Python, list index begins at zero and ends at the length of the list minus one. For example, in this list, 'Andy' is found at index 2.

names = ['Roger', 'Rafael', 'Andy', 'Novak']

List Method .append()

In Python, you can add values to the end of a list using the .append() method. This will place the object passed in as a new element at the very end of the list. Printing the list afterwards will visually show the appended value. This .append() method is not to be confused with returning an entirely new list with the passed object.

orders = ['daisies', 'periwinkle']
orders.append('tulips')
print(orders)
# Result: ['daisies', 'periwinkle', 'tulips']

List Method .remove()

The .remove() method in Python is used to remove an element from a list by passing in the value of the element to be removed as an argument. In the case where two or more elements in the list have the same value, the first occurrence of the element is removed.

# Create a list
shopping_line = ["Cole", "Kip", "Chris", "Sylvana", "Chris"]
# Removes the first occurance of "Chris"
shopping_line.remove("Chris")
print(shopping_line)
# Output
# ["Cole", "Kip", "Sylvana", "Chris"]

Tuples

Tuples are one of the built-in data structures in Python. Tuples are immutable, meaning we can’t modify a tuple’s elements after creating one, and they do not require an extra memory block like lists. Because of this, tuples are great to work with if you are working with data that won’t need to be changed in your code.

Some of the built-in methods and functions to be used with tuples are: len(), max(), min(), .index() and .count().

my_tuple = ('abc', 123, 'def', 456, 789, 'ghi')
len(my_tuple) # returns length of tuple
max(my_tuple) # returns maximum value of tuple
min(my_tuple) # returns minimum value of tuple
my_tuple.index(123) # returns the position of the value 123
my_tuple.count('abc') # returns the number of occurrences of the value 'abc'

Learn more on Codecademy

  • Learn Python programming with AI tools. This course takes you from fundamentals to best practices using Codex CLI for serious development.
    • Includes 4 Courses
    • With Certificate
    • Beginner Friendly.
      7 hours