D335 PYTHON ENDGAME FUN-LETS
QUESTIONS AND ANSWERS WITH
COMPLETE SOLUTIONS 100%
CORRECT!!!
1. Mapping CSV Pairs into a Dictionary
Scenario: You have a CSV row where data is stored in alternating pairs (e.g.,
Key1, Value1, Key2, Value2). You want to transform these into a standard Python
dictionary.
The Solution:
Python
for row in csv_reader:
# row[::2] selects every second element starting at index 0 (the keys)
# row[1::2] selects every second element starting at index 1 (the values)
row_dict = dict(zip(row[::2], row[1::2]))
print(row_dict)
How it works:
Slicing (::2): This creates a subsequence by skipping every other item.
zip(): This "zips" the two subsequences together into pairs.
dict(): This converts those pairs into key-value entries.
2. Grouping Words by Initial Letter from a File
Scenario: You have a text file where each line contains words. You need to create
a dictionary where the Key is the first letter of the line and the Value is a list of all
words on that line.
The Solution:
Python
, with open('data.txt', 'r') as f:
word_map = {}
for line in f:
# line[0] acts as the key (first character)
# line.split() creates a list of all words on that line
word_map[line[0]] = line.split()
Technical Note:
Using for line in f: is more memory-efficient than f.readlines(), as it reads the file
one line at a time rather than loading the entire file into memory at once.
Standard Dictionary Operations Summary
Goal Syntax
Initialize my_dict = {}
Add/Update my_dict[key] = value
Check for Key if key in my_dict:
Get Keys my_dict.keys()
Get Values my_dict.values()
Given a ten-digit integer, isolate the first three, next three and last four digits using
floor and modulo. -ANSWER ✔✔#shear right 7 digits
first_three = ten_digits // 10000000
#shear right 4 digits then shear left 3 digits
next_three = (ten_digits // 10000) % 1000
QUESTIONS AND ANSWERS WITH
COMPLETE SOLUTIONS 100%
CORRECT!!!
1. Mapping CSV Pairs into a Dictionary
Scenario: You have a CSV row where data is stored in alternating pairs (e.g.,
Key1, Value1, Key2, Value2). You want to transform these into a standard Python
dictionary.
The Solution:
Python
for row in csv_reader:
# row[::2] selects every second element starting at index 0 (the keys)
# row[1::2] selects every second element starting at index 1 (the values)
row_dict = dict(zip(row[::2], row[1::2]))
print(row_dict)
How it works:
Slicing (::2): This creates a subsequence by skipping every other item.
zip(): This "zips" the two subsequences together into pairs.
dict(): This converts those pairs into key-value entries.
2. Grouping Words by Initial Letter from a File
Scenario: You have a text file where each line contains words. You need to create
a dictionary where the Key is the first letter of the line and the Value is a list of all
words on that line.
The Solution:
Python
, with open('data.txt', 'r') as f:
word_map = {}
for line in f:
# line[0] acts as the key (first character)
# line.split() creates a list of all words on that line
word_map[line[0]] = line.split()
Technical Note:
Using for line in f: is more memory-efficient than f.readlines(), as it reads the file
one line at a time rather than loading the entire file into memory at once.
Standard Dictionary Operations Summary
Goal Syntax
Initialize my_dict = {}
Add/Update my_dict[key] = value
Check for Key if key in my_dict:
Get Keys my_dict.keys()
Get Values my_dict.values()
Given a ten-digit integer, isolate the first three, next three and last four digits using
floor and modulo. -ANSWER ✔✔#shear right 7 digits
first_three = ten_digits // 10000000
#shear right 4 digits then shear left 3 digits
next_three = (ten_digits // 10000) % 1000