Python - Lists
If you want to add a single element to a List using +, then what must you be sure to do? And then give some examples of this in Python.
Always be sure to put that element in brackets [ ] when adding it to a List. Give some examples of this in Python.
True or False: Lists in Python are immutable.
False. Lists are mutable! Meaning that the data types within it can be changed/altered.
What kind of data type(s) can a List contain within its brackets [ ]?
Lists can contain any data type in Python! For example, this list contains a String, Integer, Boolean, and a Float.
True or False: Lists can consist of: -Indexing -Slicing -Concatenation
True.
How would you go about adding multiple items to a List? And then give some examples of this in Python.
When you want to add multiple items to a List, you can use + to combine two lists (this is also known as Concatenation). Give some examples of this in Python.
When dealing with Lists, what is the .append() method used for? And give some examples of how this can be used in Python.
.append() method allows you to add an extra element to the end of a List. Give some examples of how to use this method in Python.
What Python method can you use to remove elements in a List?
.remove( ) method
Give examples in Python of how the .pop method can be applied to Lists.
Gives some examples in Python.
Give the three ways in Python that you can add items to a List.
1. Simply go inside of the brackets [ ] and add another comma and then another item. Give an example of this in Python. 2. Example: list_name += [Joe] Give an example of this in Python. 3. Example: heights. (TAB button) append(Joe) Give an example of this in Python.
Lists are defined and/or identified by what?
Brackets [ ]
When using Concatenation on a List, how would you make the changes to the List permanent? And then give examples of how to do this in Python.
By reassigning the List/re-defining the Variable for the List. Ex. my_list = my_list + ['add new item permanently'] Give more examples of how to do this in Python.
True or False: Lists don't have to be ordered sequences of data.
False. Lists are widely known in Python as being ordered collections of data.
True or False: the append() method in Python can add multiple items at a time to a List.
False. The append() method in Python can only add ONE item at a time to a List. In order to add more than one item, you have to use the append() method multiple times.
Can you use the len() function to tell you how many items are in the sequence of a List? If yes, give an example in Python.
Give an example of this in Python.
Suppose you want to make a list of the heights of students in a class: Noelle is 61 inches tall Ava is 70 inches tall Sam is 67 inches tall Mia is 64 inches tall In Python, you can create a Variable called -heights- to store these integers into a List. How would you go about doing this?
Give an example of this in Python.
Give some examples of how Indexing can apply to a List.
Give examples in Python.
Give some examples of how Slicing can apply to a List.
Give examples in Python.
Give some examples of how to Concatenate Lists.
Give examples in Python.
Give some examples of how you can put several Lists into one List.
Give some examples in Python.
Give some examples of how you can reverse a List in Python.
Give some examples in Python.
Give some examples of how you can sort a List in Python.
Give some examples in Python.
How would you go about adding new items to an already created List? Give some examples.
Give some examples of how to do this in Python.
Give some examples of how replace/modify different List elements using both Indexing and Reverse Indexing.
Give some examples of this in Python.
Give some examples of how to access different List elements using Indexing.
Give some examples of this in Python.
Give some examples of how to access different List elements using Reverse Indexing.
Give some examples of this in Python.
Unlike Strings, Lists are considered "mutable." What does this mean?
It means that the elements that are inside of a List can be changed/altered!
In Python, the specific location of an element inside of a List is called what?
Its Index (i.e., Indexing). Therefore, Python Lists use Indexing!
What data structure in Python allows you to work with a collection of data in sequential order?
Lists
Create a Two-dimensional (2D) List, and practice adding new items to that List.
Practice doing this in Python.
Practice using both Indexing and Reverse Indexing on Two-dimensional (2D) Lists.
Practice doing this in Python.
What happens if you use the .remove( ) method on a List that contains duplicate elements (i.e., a List that has two Chris' in it)?
Then only the first instance of the duplicate element is removed. Ex. shopping_line = ["Cole", "Kip", "Chris", "Sylvana", "Chris"] # Remove a element shopping_line.remove("Chris") print(shopping_line) Will output: ["Cole", "Kip", "Sylvana", "Chris"]
heights = [["Noelle", 61], ["Ava", 70], ["Sam", 67], ["Mia", 64]] This is an example of what type of List?
This is an example of a Two-dimensional (2D) List.
True or False: The items in a List can be numbers or Strings. But Lists can also contain other lists.
True!
True or False: A List begins and ends with square brackets [ ], and each item listed inside of it is separated by a comma.
True.
How can you use Indexing if you want to access a particular element inside of a Two-dimensional (2D) List?
Two-dimensional (2D) Lists can be accessed similar to their one-dimensional counterpart. Instead of providing a single pair of brackets [ ], we use an additional set for each dimension past the first. Ex. heights = [["Noelle", 61], ["Ali", 70], ["Sam", 67]] #Access the sublist at index 0, and then access the 1st index of that sublist. noelles_height = heights[0][1] print(noelles_height) Would output: 61 "Noelle" = heights[0][0] "Ali" = heights[1][0] "70" = heights[1][1] "Sam" = heights[2][0] "67" = heights[2][1]
Lists that contain other Lists are known as what?
Two-dimensional (2D) Lists.
Give an example of how you can remove an item from a List.
You can use the "pop" method by pressing "TAB" in order to do this. Give some examples of this in Python.