-
Notifications
You must be signed in to change notification settings - Fork 9
Open
Description
Homework: Lists
🔥Read carefully until the end before you start solving the exercises🔥
Practice the Basics 💪🏻
Empty, Pre-populated, and Lists within Lists
You can uncomment or type the necessary code on each task
---------------------------------------------------------------------
Task 1. Create three lists:
List #1: Create an empty list and then use append() to populate it with the names of three of your friends.
List #2: Create the same list, but use the syntax to create it pre-populated.
List #3: Create the same list, but each element should be a list,
where the first sub-element is the friend's name
and the second sub-element is their age.
List 1:
list_1 = []
list_1.append('Jane')
list_1.append('Janet')
list_1.append('Jenny')
List 2:
list_2 = [Jane, Janet, Jenny]
List 3:
list_3 = [['Jane',22], ['Janet',24], ['Jenny',26]]
'Jane', 22
'Janet', 24
'Jenny', 26
]
---------------------------------------------------------------------
Task 2. Retrieve elements from a List
Create print statements to retrieve the following elements from the previous lists:
- From List 2: Retrieve the name of the second friend.
- From List 3: Retrieve the age of the last friend you put in the list.
Name of second friend
second_friend_name = list_2['Janet']
Age of the last friend of the list
last_friend_age = list_3[;Jenny']['26']
---------------------------------------------------------------------
Task 3. Remove elements from a List
From the lists provided, remove the requested elements. Easy peazy.
cities = ["Houston", "Dallas", "Austin"]
fruits = ["apple", "banana", "orange"]
Remove Austin from cities without using its index
cities.remove("Austin")
Remove the last element from fruits using negative indexes
print(fruits[-1])
---------------------------------------------------------------------
Task 4. Verify if an element exists in a list
Given the provided list, write code that prints YES if the list contains the word cheese
The list
pantry = ["ham", "bread", "cheese"]
Write code that prints YES if the list contains "cheese".
if "cheese" in pantry:
# print('YES')
---------------------------------------------------------------------
Task 5. Sorting and Reversing
Given the provided list, write code that sorts and reverses it, as required.
numbers = [6, 34, 17, 9, 2, 11, 57, 9, 32]
Write code that sorts the list in ascending order without disturbing the original.
sorted_numbers = sorted(numbers)
print(sorted_numbers)
Write code that reverses (flips) the list without disturbing the original.
Remember that in this case, casting is required.
reversed_numbers = list(reversed(numbers))
print(reversed_numbers)
Write code that sorts the list in place, modifying the original.
numbers.sort()
print(numbers)
Write code that reverses (flips) the list in place, modifying the original.
numbers.reverse()
print(numbers)
---------------------------------------------------------------------
Task 6. Stitching and Slicing
You are given two lists with names of days of the week:
- work_days= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
- rest_days ['Saturday', 'Sunday']
Create a third list that contains the concatenation of the previous two.
Call it 'full_week'
full_week = work_days + rest_days
print(full_week)
Now, write python code that prints a slice from 'full_week' with the work days.
work_days = ['mon', 'tue', 'wed', 'thu', 'fri']
rest_days = ['sat', 'sun']
Concatenate work_days and rest_rays
full_week = work_days + rest_days
Slice with the work days
print(full_week[0.5])
---------------------------------------------------------------------
Task 7. Aggregators and Helpers
Given a list of numbers, use helpers and aggregators to answer the questions:
- What's the lowest number?
- What's the highest number?
- What's the sum of all the numbers in the list?
- How many times is the number 9 in the list?
- How many total elements are in the list?
numbers = [6, 34, 17, 9, 2, 11, 57, 9, 32]
Lowest number
print(min(numbers))
Highest number
print(max(numbers))
Sum of everything
print(sum(numbers))
Count number 9s
print(numbers.count(9))
Total number of elements
print(len(numbers))
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels