8.7 KiB
8.7 KiB
In [ ]:
# Assign a list to an variable named my_list
my_list = [1,2,3]In [ ]:
my_list = ['A string',23,100.232,'o']In [ ]:
len(my_list)In [ ]:
my_list = ['one','two','three',4,5]In [ ]:
# Grab element at index 0
my_list[0]In [ ]:
# Grab index 1 and everything past it
my_list[1:]In [ ]:
# Grab everything UP TO index 3
my_list[:3]In [ ]:
my_list + ['new item']In [ ]:
my_listIn [ ]:
# Reassign
my_list = my_list + ['add new item permanently']In [ ]:
my_listIn [ ]:
# Make the list double
my_list * 2In [ ]:
# Again doubling not permanent
my_listIn [ ]:
# Create a new list
list1 = [1,2,3]In [ ]:
# Append
list1.append('append me!')In [ ]:
# Show
list1In [ ]:
# Pop off the 0 indexed item
list1.pop(0)In [ ]:
# Show
list1In [ ]:
# Assign the popped element, remember default popped index is -1
popped_item = list1.pop()In [ ]:
popped_itemIn [ ]:
# Show remaining list
list1In [ ]:
list1[100]In [ ]:
new_list = ['a','e','x','b','c']In [ ]:
#Show
new_listIn [ ]:
# Use reverse to reverse order (this is permanent!)
new_list.reverse()In [ ]:
new_listIn [ ]:
# Use sort to sort the list (in this case alphabetical order, but for numbers it will go ascending)
new_list.sort()In [ ]:
new_listIn [ ]:
# Let's make three lists
lst_1=[1,2,3]
lst_2=[4,5,6]
lst_3=[7,8,9]
# Make a list of lists to form a matrix
matrix = [lst_1,lst_2,lst_3]In [ ]:
# Show
matrixIn [ ]:
# Grab first item in matrix object
matrix[0]In [ ]:
# Grab first item of the first item in the matrix object
matrix[0][0]