Today's lesson in 11 (!) parts is Lists. Seems pretty important if they use 11 lessons to accomplish this.
Lists:
- Begin and end with square brackets [ ]
- Each item is separated by a comma ,
- For readability and cleanliness I will insert a space after each comma
This is a list
A list without comma separators will not run.
Lists can contain any type of data available in Python! :)
You can just create an empty list like this:
Combining lists with the zip() function
names_and_heights = zip(names, heights)
If we print this we'd get this:
<zip object at 0x7f938fj6848>
What this means is that this is where the zip object is stored in the computer memory.
Use list to display the zipped object like this:
converted_list = list(names_and_heights)
print(converted_list)
When doing this, we get the following output (partial):
[('Jenny', 61), ('Alexus', 70), ...
Note that the inner item is a parenthesis and not a bracket. This means it's been converted to a tuple. From the python documentation here, a tuple consists of a number of values separated by commas.
Got the challenge on the first try!
grocery_list = ["mango", "pineapple", "banana"]
grocery_list.insert(0, "berries")
print(grocery_list)
Output becomes:
['berries', 'mango', 'pineapple', 'banana']
Exercise here:
The .pop() method removes a single item from the specified index.
Using .pop() without an index removes the last element in the list
When printing this list it will actually list the removed item below the list
Using .pop() with an index and without:
range() = takes a single input, generates numbers starting at 0 and ending at the number before the input (because it uses 0 as a number). Example below:
my_range = range(10)
print(list(my_range))
When you create a range, it is an object. When you call it, you'll have to use list() to convert it.
Range, continued: You can define a range to start at a specific number and end before your specified number and skip numbers like this:
skip_by_five = range(1, 50, 5)
This means start the list at 1, go up to 49, and list all numbers that fit the rule of skipping by fives
Finding length of a range is not hard using len() method:
Slicing a list:
Use brackets with a beginning and ending index to determine slice. Remember that the end is one more than you actually need.
More slicing: only displaying last 2 elements and slicing off last 3 elements, in order:
Counting in a list with .count()
Sorting a list with .sort()
The difference between .sort() and sorted()
sorted() comes before a list, and makes a new list rather than modifying the one that already exists
Practicing lists:
Many iterations to get here. The # under function def is what I had originally tried and made too complicated. I got it to return 3, but I couldn't figure out how to get it to append the 3 to the list. I need to get into the mentality of writing less... I am in the baby stages of learning right now. It's ok.
Got this one on the second time because I didn't read the directions about doing it 3 times at first:
Again, not reading directions has me redoing work. I missed the part about the length of list.
Had to refresh myself on how to use sorted() by scrolling up on this post, but I got it!
Had to get help on this one.
Large project: I did get about 60% of this on my own. Slicing is an issue for me.
More about tuples:
It's like a list, but you cannot change anything in the tuple after creation.
When creating a one element tuple, you have to add a comma after the single element:
one_element_tuple = (4,)
You use tuples where you don't want the order to change, or when things need to stay in one way.
And that is the end of the lists lesson....




























