Skip to content

Instantly share code, notes, and snippets.

@cibofdevs
Last active March 27, 2023 03:09
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cibofdevs/1371cf1566c567f2e98bba8739f32e50 to your computer and use it in GitHub Desktop.
Save cibofdevs/1371cf1566c567f2e98bba8739f32e50 to your computer and use it in GitHub Desktop.
# 1. The variable nested contains a nested list. Assign ‘snake’ to the variable output using indexing.
nested = [['dog', 'cat', 'horse'], ['frog', 'turtle', 'snake', 'gecko'], ['hamster', 'gerbil', 'rat', 'ferret']]
output = nested[1][2]
print(output)
# 2. Below, a list of lists is provided. Use in and not in tests to create variables with Boolean values.
# See comments for further instructions.
lst = [['apple', 'orange', 'banana'], [5, 6, 7, 8, 9.9, 10], ['green', 'yellow', 'purple', 'red']]
#Test to see if 'yellow' is in the third list of lst. Save to variable ``yellow``
yellow = 'yellow' in lst[2]
#Test to see if 4 is in the second list of lst. Save to variable ``four``
four = 4 in lst[1]
#Test to see if 'orange' is in the first element of lst. Save to variable ``orange``
orange = 'orange' in lst[0]
# 3. Below, we’ve provided a list of lists. Use in statements to create variables with Boolean values
# see the ActiveCode window for further directions.
L = [[5, 8, 7], ['hello', 'hi', 'hola'], [6.6, 1.54, 3.99], ['small', 'large']]
# Test if 'hola' is in the list L. Save to variable name test1
test1 = 'hola' in L
# Test if [5, 8, 7] is in the list L. Save to variable name test2
test2 = [5, 8, 7] in L
# Test if 6.6 is in the third element of list L. Save to variable name test3
test3 = 6.6 in L[2]
# 4. Provided is a nested data structure. Follow the instructions in the comments below. Do not hard code.
nested = {'data': ['finding', 23, ['exercises', 'hangout', 34]], 'window': ['part', 'whole', [], 'sum', ['math', 'calculus', 'algebra', 'geometry', 'statistics',['physics', 'chemistry', 'biology']]]}
# Check to see if the string data is a key in nested, if it is, assign True to the variable data, otherwise assign False.
if 'data' in nested:
data = True
else:
data = False
# Check to see if the integer 24 is in the value of the key data, if it is then assign to the variable twentyfour the value of True, otherwise False.
if 24 in nested:
twentyfour = True
else:
twentyfour = False
# Check to see that the string 'whole' is not in the value of the key window. If it's not, then assign to the variable whole the value of True, otherwise False.
if 'whole' in nested:
whole = True
else:
whole = False
# Check to see if the string 'physics' is a key in the dictionary nested. If it is, assign to the variable physics, the value of True, otherwise False.
if 'physics' in nested:
physics = True
else:
physics = False
# 5. The variable nested_d contains a nested dictionary with the gold medal counts
# for the top four countries in the past three Olympics.
# Assign the value of Great Britain’s gold medal count from the London Olympics to the variable london_gold.
# Use indexing. Do not hardcode.
nested_d = {'Beijing':{'China':51, 'USA':36, 'Russia':22, 'Great Britain':19}, 'London':{'USA':46, 'China':38, 'Great Britain':29, 'Russia':22}, 'Rio':{'USA':35, 'Great Britain':22, 'China':20, 'Germany':13}}
london_gold = nested_d['London']['Great Britain']
# 6. Below, we have provided a nested dictionary.
# Index into the dictionary to create variables that we have listed in the ActiveCode window.
sports = {'swimming': ['butterfly', 'breaststroke', 'backstroke', 'freestyle'], 'diving': ['springboard', 'platform', 'synchronized'], 'track': ['sprint', 'distance', 'jumps', 'throws'], 'gymnastics': {'women':['vault', 'floor', 'uneven bars', 'balance beam'], 'men': ['vault', 'parallel bars', 'floor', 'rings']}}
# Assign the string 'backstroke' to the name v1
v1 = sports['swimming'][2]
# Assign the string 'platform' to the name v2
v2 = sports['diving'][1]
# Assign the list ['vault', 'floor', 'uneven bars', 'balance beam'] to the name v3
v3 = sports['gymnastics']['women']
# Assign the string 'rings' to the name v4
v4 = sports['gymnastics']['men'][3]
print(v4)
# 7. Given the dictionary, nested_d,
# save the medal count for the USA from all three Olympics in the dictionary to the list US_count.
nested_d = {'Beijing':{'China':51, 'USA':36, 'Russia':22, 'Great Britain':19}, 'London':{'USA':46, 'China':38, 'Great Britain':29, 'Russia':22}, 'Rio':{'USA':35, 'Great Britain':22, 'China':20, 'Germany':13}}
US_count = []
US_count.append(nested_d['Beijing']['USA'])
US_count.append(nested_d['London']['USA'])
US_count.append(nested_d['Rio']['USA'])
# 8. Iterate through the contents of l_of_l and assign the third element of sublist to a new list called third.
l_of_l = [['purple', 'mauve', 'blue'], ['red', 'maroon', 'blood orange', 'crimson'], ['sea green', 'cornflower', 'lavender', 'indigo'], ['yellow', 'amarillo', 'mac n cheese', 'golden rod']]
third = [i[2] for i in l_of_l]
# 9. Given below is a list of lists of athletes.
# Create a list, t, that saves only the athlete’s name if it contains the letter “t”.
# If it does not contain the letter “t”, save the athlete name into list other.
athletes = [
['Phelps', 'Lochte', 'Schooling', 'Ledecky', 'Franklin'],
['Felix', 'Bolt', 'Gardner', 'Eaton'],
['Biles', 'Douglas', 'Hamm', 'Raisman', 'Mikulak', 'Dalton']
]
t = []
other = []
for list in athletes:
for char in list:
if 't' in char:
t.append(char)
else:
other.append(char)
@seanred111
Copy link

#7 could be answered like this:

nested_d = {'Beijing':{'China':51, 'USA':36, 'Russia':22, 'Great Britain':19}, 'London':{'USA':46, 'China':38, 'Great Britain':29, 'Russia':22}, 'Rio':{'USA':35, 'Great Britain':22, 'China':20, 'Germany':13}}

US_count = []

US_count.append(nested_d['Beijing']['London']['Rio']['USA'])

@RichardModad
Copy link

Even better for #7

US_count = []
for year in nested_d:
US_count.append(nested_d[year]['USA'])

@advika2408
Copy link

advika2408 commented Jun 8, 2021

For #7 you could also write ---

nested_d = {'Beijing':{'China':51, 'USA':36, 'Russia':22, 'Great Britain':19}, 'London':{'USA':46, 'China':38, 'Great Britain':29, 'Russia':22}, 'Rio':{'USA':35, 'Great Britain':22, 'China':20, 'Germany':13}}

US_count = []

for olympic_countries in nested_d : #this gets Beijing, London, and Rio
US_count.append(nested_d[olympic_countries]['USA']

@wel51x
Copy link

wel51x commented Aug 2, 2021

seanred111 said:

#7 could be answered like this:

nested_d = {'Beijing':{'China':51, 'USA':36, 'Russia':22, 'Great Britain':19}, 'London':{'USA':46, 'China':38, 'Great Britain':29, 'Russia':22}, 'Rio':{'USA':35, 'Great Britain':22, 'China':20, 'Germany':13}}

US_count = []

US_count.append(nested_d['Beijing']['London']['Rio']['USA'])

Not sure about this. I get:
KeyError: 'London'

@abishekachazhoor123
Copy link

abishekachazhoor123 commented Aug 23, 2021

nested_d = {'Beijing':{'China':51, 'USA':36, 'Russia':22, 'Great Britain':19}, 'London':{'USA':46, 'China':38, 'Great Britain':29, 'Russia':22}, 'Rio':{'USA':35, 'Great Britain':22, 'China':20, 'Germany':13}}

US_count=[]
country=nested_d.keys()
for each_country in country:
US_count.append(nested_d[each_country]['USA'])

@hadrocodium
Copy link

hadrocodium commented Oct 26, 2021

Question number 7.

Using list comprehension reduces the code to one line.

US_count = [nested_d[i]['USA'] for i in nested_d]

But pylint does not like the code above. It prefers the usage of .items() method.

US_count = []

for olympics, country_medal in nested_d.items():
    for country, medal in country_medal.items():
        if country == "USA":
            US_count.append(medal)

Or the use of the list comprehension.

US_count = [
    medal
    for olympics, country_medal in nested_d.items()
    for country, medal in country_medal.items()
    if country == "USA"
    ]

@jebandshadow
Copy link

For question 4 you have checked if the variable is in nested, not if it is in data. You can check using:
if 24 in nested['data']:
twentyfour = True
else:
twentyfour = False

The same goes for similar situations in this question

@polars2022
Copy link

I have two questions about question number 2.
Question two askes for Boolean values as answers. How are the below code lines, which are correct answers, Boolean Values?
yellow=‘yellow’ in lst[2]
four=4 in lst[1]
orange=‘orange’ in lst[0]

Also,
All three parts of question 2 will accept code in this structure as a correct answer:
if 'yellow' in lst[2]:
yellow=True
else:
yellow=False

However, only the second part of question 2 will accept code with the structure below:
for element in lst[0]:
if element==4:
four=True
else:
four=False

If I try substitutuing 'orange' or 'yellow' in for 4 and change to the appropriate index number then it won't work for the first and third part of the question. Why?

@polars2022
Copy link

Three questions on question number 3, pasted below with cibofdevs' simple and beautiful code:

3. Below, we’ve provided a list of lists. Use in statements to create variables with Boolean values

see the ActiveCode window for further directions.

L = [[5, 8, 7], ['hello', 'hi', 'hola'], [6.6, 1.54, 3.99], ['small', 'large']]

Test if 'hola' is in the list L. Save to variable name test1

test1 = 'hola' in L

Test if [5, 8, 7] is in the list L. Save to variable name test2

test2 = [5, 8, 7] in L

Test if 6.6 is in the third element of list L. Save to variable name test3

test3 = 6.6 in L[2]

I can't understand how cibofdevs' code works at this point, I just started learning python a few months ago. So I wrote ugly code that works, I have questions on on my code bracketed in #**** below:

L = [[5, 8, 7], ['hello', 'hi', 'hola'], [6.6, 1.54, 3.99], ['small', 'large']]

Test if 'hola' is in the list L. Save to variable name test1

if 'hola' in L:
test1=True
else:
test1=False

****This structure above only works for part one of this question, why doesn't it work for parts two and three? ****#

Test if [5, 8, 7] is in the list L. Save to variable name test2

test2=False
if [5, 8, 7] in L:
test2=True
#How can this work with test2=False above and outside of the if statement?#

Test if 6.6 is in the third element of list L. Save to variable name test3

if 6.6 in L[2]:
test3=True
#How can this work with no test3=False statement anywhere?#

@idamou
Copy link

idamou commented May 10, 2022

thank you so much

@OliviaLivWen
Copy link

thanks a lot for sharing. It's really helpful!
contribute my code too.

8 Iterate through the contents of l_of_l and assign the third element of sublist to a new list called third.

1_of_1 = [['purple', 'mauve', 'blue'], ['red', 'maroon', 'blood orange', 'crimson'], ['sea green', 'cornflower', 'lavender', 'indigo'], ['yellow', 'amarillo', 'mac n cheese', 'golden rod']]

third = []
for lst in 1_of_1:
temp = lst[2]
third.append(temp)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment