List are:
List examples:
my_list = [1,2,3,4]
my_list2 = [‘STRING’,100,23.3]
Cancatinate 2 list
mylist = [‘one’,’two’,’three’]
another_list = [‘apple’,’pie’,’cherry’]
mylist + another_list
Combine two list
mylist = [‘one’,’two’,’three’]
another_list = [‘apple’,’pie’,’cherry’]
mynewlist = mylist + another_list
Change the first string in this list to “One for all’
new_list = [‘one’, ‘two’, ‘three’, ‘four’, ‘five’]
new_list[0] = ‘One for all’
would change it to
new_list = [‘One for all’, ‘two’, ‘three’, ‘four’, ‘five’]
Add an element to the end of a list:
new_list.
new_list.append(‘six’)
new_list = [‘One for all’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’]
NOTE:This affects the list in place
Remove the last element from a list:
Done using pop
new_list.pop()
new_list = [‘One for all’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’]
Remove an element from the 3 position
new_list = [‘One for all’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’]
new_list.pop(3)
new_list = [‘One for all’, ‘two’, ‘three’, ‘five’, ‘six’]
Sort list
new_list = [‘a’, ‘e’, ‘x’, ‘b’, ‘c’]
num_list = [4,1,8,3]
Call the sort method
new_list.sort()
NOTE:This sorts the list in place
Reassign this list but first Sort list
new_list = [‘a’, ‘e’, ‘x’, ‘b’, ‘c’]
num_list = [4,1,8,3]
new_list.sort()
my_sorted_list = new_list
Reverse this list
new_list = [‘a’, ‘e’, ‘x’, ‘b’, ‘c’]
num_list = [4,1,8,3]
[3,8,1,4]
[‘c’, ‘b’, ‘e’, ‘a’]
How do I index a nested list? For example if I want to grab 2 from [1,1,[1,2]]?
You would just add another set of brackets for indexing the nested list, for example:
my_list[2][1]
NOTE: List follow the same numbering sequence as strings.
If lst=[0,1,2] what is the result of lst.pop()
2
Lists can have multiple object types.
True
If lst=[‘a’,’b’,’c’] What is the result of lst[1:]?
[‘b’,’c’]
Are list mutable?
What are the immutable data types in Python?
What are the things inside a list called?
Each item is called an Element
Insert an element into a list
motorcycles = ['honda', 'yamaha', 'suzuki'] motorcycles.insert(0, 'ducati') print(motorcycles) ['ducati', 'honda', 'yamaha', 'suzuki']
What are the 2 things need to Insert an element into a list?
motorcycle.insert(3, "harley davidson") print(motorcycle) ['suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda'] motorcycle.insert(0, "triumph") print(motorcycle) ['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
How do remove an element from a list?
Delete an element permanently a list
del motorcycle[2]
motorcycle = ['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda'] print(motorcycle) ['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda'] del motorcycle[2] print(motorcycle) ['triumph', 'suzuki', 'kawasiki', 'harley davidson', 'honda']
How do remove the last item from a list for later use?
motorcycle = ['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
print(motorcycle)
['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
popped_motorcycle = motorcycle.pop()
print(f"The last motorcyle I owneed was a {popped_motorcycle.title()}.")
The last motorcyle I owneed was a Honda.
print(popped_motorcycle)
hondaHow do remove an item from anywhere in list for later use?
# Remove an element for later use in a list from any where in the list
motorcycle = ['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
print(motorcycle)
['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
popped_motorcycle = motorcycle.pop(3)
print(f"The last motorcyle I owneed was a {popped_motorcycle.title()}.")
The last motorcyle I owneed was a Kawasiki.
print(popped_motorcycle)
kawasikiWhat is the difference between append() and insert()?