Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
Fall 2021 Dr. Zuhair Zafar [email protected]
In general, we can create lists via putting a number of expressions in square brackets List = [] List = [expression, …] E.g., List = [1+2, 7, “Eleven”] List = [expression for variable in sequence], where expression is evaluated once for every element in the sequence (this is called ”list comprehension”) E.g., List1 = [x for x in range(10)] E.g., List2 = [x + 1 for x in range(10)] E.g., List3 = [x for x in range(10) if x % 2 == 0]
L1 = [1, 2, 3] L2 = [1, 2, 3] L1 == L True L1 is L False
True
L3 is L True
L1 and L2 have the same values but they are independent lists (i.e., they do not point to the same list) L3 and L4 point to the same list
Similar to strings as well, lists can be sliced To slice elements within a range start_index and end_index (inclusive), we can use [start_index : end_index+1] A B C D F 0 1 2 3 4
grades = ["A", "B", "C", "D", "F"] grades[1:3] ['B', 'C']
Similar to strings as well, lists can be sliced To slice elements from the beginning to a certain ending index (inclusive), we can use [: end_index+1] A B C D F 0 1 2 3 4
grades = ["A", "B", "C", "D", "F"] grades[:3] ['A', 'B', 'C']
Similar to strings as well, lists can be sliced To slice all elements, we can use [:] A B C D F 0 1 2 3 4
grades = ["A", "B", "C", "D", "F"] grades[:] ['A', 'B', 'C', 'D', 'F']
Similar to strings as well, lists can be sliced We can even pass a negative index, after which Python will add the length of the list to the index A B C D F 0 1 2 3 4
grades = ["A", "B", "C", "D", "F"] grades[-1] 'F'
Similar to strings as well, lists can be sliced A B C D F 0 1 2 3 4
grades = ["A", "B", "C", "D", "F"] grades[:0] []
If you want to start from - 1 (inclusive), pass nothing to the ending index
Similar to strings as well, lists can be sliced To slice in steps, we can use [start_index, end_index+1, step] (idea applies to slicing from forward and backward) A B C D F 0 1 2 3 4
grades = ["A", "B", "C", "D", "F"] grades[0:5:2] grades = ["A", "B", "C", "D", "F"] >>> grades[1:3] ['B', 'C'] >>> ## Slicing Lists Similar to strings as well, lists can be sliced To slice elements from the beginning to a certain ending index (inclusive), we can use [: end_index+1] A B C D F 0 1 2 3 4 >>> grades = ["A", "B", "C", "D", "F"] >>> grades[:3] ['A', 'B', 'C'] >>> ## Slicing Lists Similar to strings as well, lists can be sliced To slice all elements, we can use [:] A B C D F 0 1 2 3 4 >>> grades = ["A", "B", "C", "D", "F"] >>> grades[:] ['A', 'B', 'C', 'D', 'F'] >>> ## Slicing Lists Similar to strings as well, lists can be sliced We can even pass a negative index, after which Python will add the length of the list to the index A B C D F 0 1 2 3 4 >>> grades = ["A", "B", "C", "D", "F"] >>> grades[-1] 'F' >>> ## Slicing Lists Similar to strings as well, lists can be sliced A B C D F 0 1 2 3 4 >>> grades = ["A", "B", "C", "D", "F"] >>> grades[:0] [] >>> If you want to start from - 1 (inclusive), pass nothing to the ending index ## Slicing Lists Similar to strings as well, lists can be sliced To slice in steps, we can use [start_index, end_index+1, step] (idea applies to slicing from forward and backward) A B C D F 0 1 2 3 4 >>> grades = ["A", "B", "C", "D", "F"] >>> grades[0:5:2] ['A', 'C', 'F']
Lists can have duplicate values We can also iterate over the items of any list
myList = [5, 2, 2, 3] myList [5, 2, 2, 3]
myList = [1, 2, 3, 4, 5] for i in myList: print(i, end = " ") print()
Multi-Dimensional Lists