<jupyter_start><jupyter_text>MODULE 1 - Basic Python programmingA dictionary in Python is a collection of key-value pairs. Each key is connected to its value, and you can use a key to access the value associated with that key. A key's value can be anumber, a string, a list, or even another dictionary. A key-value pair is a set of values associated with each other. When you provide a key, Pytonreturns the value associated with that key.   A simple dictionary example<jupyter_code># Store info about a particular home
home_attributes = {"color": "white", "sqft": 1850}

# To get the value associated with a key, use the name of the dictionary and place the key inside square-brackets
print(home_attributes["color"])
print(home_attributes["sqft"])

square_feet = home_attributes["sqft"]
print(f"The home has {square_feet} square feet.")

# Can also start with an empty dictionary

home_attributes = {}

home_attributes["color"] = "white"
home_attributes["sqft"] = 1850

print(home_attributes)<jupyter_output>{'color': 'white', 'sqft': 1850}<jupyter_text>Adding new key-value pairsDictionaries are dynamic structures, and you can add new key-value pairs to a dictionary at any time.<jupyter_code>home_attributes = {"color": "white", "sqft": 1850}

print(home_attributes)

# Add two new key value pairs
home_attributes["bedrooms"] = 3
home_attributes["bathrooms"] = 2

print(home_attributes)    # Now dictionary has 4 key-value pairs<jupyter_output>{'color': 'white', 'sqft': 1850}
{'color': 'white', 'sqft': 1850, 'bedrooms': 3, 'bathrooms': 2}<jupyter_text>Modifying values in a dictionary<jupyter_code>home_attributes = {"color": "white", "sqft": 1850}
print(home_attributes)

home_attributes["color"] ="brown"
print(home_attributes)

# Modifying values in a dictionary with logic
home_attributes = {"bonus_pts": 2, "sqft": 1850, "bedrooms": 3, "bathrooms": 2, "extras": "pool"}
print(home_attributes)

if home_attributes["extras"] == "balcony":
    add_bonus_pts = 1
elif home_attributes["extras"] == "pool":
    add_bonus_pts = 2
else:
    add_bonus_pts = 3
       
home_attributes["bonus_pts"] = home_attributes["bonus_pts"] + add_bonus_pts

print(f"New bonus points: {home_attributes['bonus_pts']}")

# Removing key-value pairs in a dictionary

home_attributes = {"color": "white", "sqft": 1850}
print(home_attributes)

del home_attributes['color']
print(home_attributes)<jupyter_output>{'color': 'white', 'sqft': 1850}
{'sqft': 1850}<jupyter_text>Looping through dictionaries<jupyter_code>favorite_languages = {
    'ellen': 'Python',
    'stephen': 'R',
    'catherine': 'Julia',
    'ashleigh': 'Python',
    }

friends = ['ashleigh', 'stephen']
for name in favorite_languages.keys():
    print(name.title())
    
    if name in friends:
        language = favorite_languages[name].title()
        print(f"\t{name.title()}, I see you love {language}!")

# Using the get() method for dictionaries

home_attributes = {"color": "white", "sqft": 1850}

# print(home_attributed['bedrooms'])     # Throws an error

# Instead, use get() method to avoid error
bedrooms = home_attributes.get('bedrooms', 'No bedrooms value assigned.')
print(bedrooms)

bedrooms = home_attributes.get('bedrooms')   # Without 2nd arg, get "None"
print(bedrooms)<jupyter_output>No bedrooms value assigned.
None<jupyter_text>Looping through all key-value pairsDictionaries can contain large amounts of data, so sometimes you may need to loop through the dictionary. You can loop through all of a dictionary's key-value pairs, through its keys, or through its values.<jupyter_code># Loop through all key-value pairs in dictionary

data_professional = {
    'jobdesc': 'data scientist',
    'first': 'Hilary',
    'last': 'Mason',
    }

# Loop assigns each pair to the two variables
for key, value in data_professional.items():
    print(f"\nKey: {key}")
    print(f"Value: {value}")

# Now loop through favorite_languages dictionary

favorite_languages = {
    'ellen': 'Python',
    'stephen': 'R',
    'catherine': 'Julia',
    'ashleigh': 'Python',
    }

for name, language in favorite_languages.items():
    print(f"{name.title()}'s favorite language is {language.title()}.")
# Looping through all the keys in a dictionary

favorite_languages = {
    'ellen': 'Python',
    'stephen': 'R',
    'catherine': 'Julia',
    'ashleigh': 'Python',
    }

# Print names of everyone who took the favorite language poll
for name in favorite_languages.keys():      # Note: you can leave off keys() method
    print(name.title())
    
# You can access the value associated with any key you care about inside the loop by using the current key.

favorite_languages = {
    'ellen': 'Python',
    'stephen': 'R',
    'catherine': 'Julia',
    'ashleigh': 'Python',
    }

friends = ['ellen', 'ashleigh']
for name in favorite_languages.keys():
    print(f"Hi {name.title()}.")
    
    if name in friends:
        language = favorite_languages[name].title()
        print(f"\t{name.title()}, I see you love {language}!")
        
# Use keys() method to query dictionary

favorite_languages = {
    'ellen': 'Python',
    'stephen': 'R',
    'catherine': 'Julia',
    'ashleigh': 'Python',
    }

# Check if 'Karin' is in the list
if 'Karin' not in favorite_languages.keys():
    print("Karin, please take the data science language poll!")
# Looping through a dictionary's keys in particular order

favorite_languages = {
    'ellen': 'Python',
    'stephen': 'R',
    'catherine': 'Julia',
    'ashleigh': 'Python',
    }

# Sort the keys before looping through them
for name in sorted(favorite_languages.keys()):
    print(f"{name.title()}, thank you for taking the data science language poll!")
    
# Now loop through the dictionary values
print("The following data science languages have been mentioned")
for language in favorite_languages.values():
    print(language.title())
    
# Now loop through the dictionary values (languages) without repeats
# When you wrap set() around a list the contains duplicate items, Python identifies the unique items in the list
# and builds a set from those items.
for language in set(favorite_languages.values()):
    print(language.title())<jupyter_output>Ashleigh, thank you for taking the data science language poll!
Catherine, thank you for taking the data science language poll!
Ellen, thank you for taking the data science language poll!
Stephen, thank you for taking the data science language poll!
The following data science languages have been mentioned
Python
R
Julia
Python
Julia
R
Python<jupyter_text>Creating a set directly<jupyter_code># A set is distinct from a dictionary even though you define both with curly brackets. 
# Sets do not have key-value pairs

languages = {
    'Python',
    'R',
    'Julia',
    'Python',
    }

languages<jupyter_output><empty_output><jupyter_text>NestingSometimes you may wish to store multiple dictionaries in a list, or a list of items as a value in a dictionary. This is called nesting. You can nest dictionaries inside a list, a list of items inside a dictionary, or even a dictionary inside another dictionary. It's common to store a number of dictionaries in a list when each dictionary contains many kinds of information about one object. All of the dictionaries in the list should have an identical structure so you can loop through ther list and work with each dictionary object in the same way.<jupyter_code>home_attributes = []

# Make room for 50 homes
for home_number in range(50):
    new_home = {'sqft': 1750, 'bedrooms': 3, 'pool': False}
    home_attributes.append(new_home)

# Print first 5 homes
for home in home_attributes[:5]:
    print(home)

# Show how many homes have been created
print(f"Total number of homes: {len(home_attributes)}")


# Now how you might use the generated group of homes
 for home in home_attributes[:3]:
    if home['pool'] == False:
        home['pool'] = True
        home['bedrooms'] = 5
        
# Display 1st 10 homes
for home in home_attributes[:10]:
    print(home)<jupyter_output>{'sqft': 1750, 'bedrooms': 3, 'pool': False}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}
Total number of homes: 50
{'sqft': 1750, 'bedrooms': 5, 'pool': True}
{'sqft': 1750, 'bedrooms': 5, 'pool': True}
{'sqft': 1750, 'bedrooms': 5, 'pool': True}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}
{'sqft': 1750, 'bedrooms': 3, 'pool': False}<jupyter_text>A list in a dictionaryRather than putting a dictionary in a list, it's sometimes useful to put a list inside a dictionary. This allows for typical 1:m (one-to-many) data relationships, e.g. 1 pizza with many toppings.<jupyter_code># Store information about a pizza being ordered.
pizza = {
    'crust': 'thick',
    'toppings': ['sausage', 'mushrooms', 'extra cheese'],
    }

# Summarize the order.
print(f"You ordered a {pizza['crust']}-crust pizza "
    "with the following toppings:")

for topping in pizza['toppings']:
    print("\t" + topping)

# You can next a lsit inside a dictionary any time you want more than one value 
# to be associated with a single key in a dictionary.

favorite_languages = {
    'ellen': ['Python', 'R'],
    'stephen': ['R', 'SQL'],
    'catherine': ['Julia'],
    'ashleigh': ['Python', 'Scala', 'Java'] 
    }

for name, languages in favorite_languages.items():
    print(f"\n{name.title()}'s favorite languages are:")
    for language in languages:
        print(f"\t{language.title()}")<jupyter_output>Ellen's favorite languages are:
	Python
	R

Stephen's favorite languages are:
	R
	Sql

Catherine's favorite languages are:
	Julia

Ashleigh's favorite languages are:
	Python
	Scala
	Java<jupyter_text>A dictionary in a dictionaryYou can nest a dictionary inside another dictionary, but your code can get complicated quickly when you do.<jupyter_code>stocks = {
    'NVIDIA': {
        'exchange': 'NASDAQ',
        'price': 499.30,
        'symbol': 'NVDA',
        },

    'Teradata': {
        'exchange': 'NYSE',
        'price': 22.55,
        'symbol': 'TDC',
        },

    }

for stock, stock_info in stocks.items():
    print(f"\nStock name: {stock}")
    exchange_symbol = f"{stock_info['exchange']} {stock_info['symbol']}"
    price = stock_info['price']
    print(f"\tExchange/symbol: {exchange_symbol.title()}")
    print(f"\tPrice: {price}")<jupyter_output>Stock name: NVIDIA
	Exchange/symbol: Nasdaq Nvda
	Price: 499.3

Stock name: Teradata
	Exchange/symbol: Nyse Tdc
	Price: 22.55
{'fraction_non_alphanumeric': 0.12639612807148176, 'fraction_numerical': 0.016288160833953836, 'mean_word_length': 4.465412004069176, 'pattern_counts': {'":': 17, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '91990', 'license': 'MIT', 'n_tokens_mistral': 3591, 'n_tokens_neox': 3324, 'n_words': 1196, 'path': 'python/MODULE1_dictionaries.ipynb', 'repo_name': 'AMULETAnalytics/UCLAIntroDataScience', 'size': '10744'}