Python - Lists and Tuples
How do you loop through a list?
>>> for item in alist: ... print(item) ... a b c d
What is a list?
A List is an ordered collection of Elements. Elements added to a List are assigned an implicit index.
What is a list comprehension?
A compact way of defining a list in one line of code. [<return> <for loop statement> <optional if statement>] >>> alist ['a', 'b', 'c', 'd'] >>> upper = [item.upper() for item in alist] >>> upper ['A', 'B', 'C', 'D'] >>> alpha = [item for item in upper if "A" in item] >>> alpha ['A']
What can you do with a copy of a list?
A copy of a list lets you work with the contents of the copied list without affecting the original list. This is because variables in python are reference pointers to data. alist = blist, will result in both variables pointing to the same data. >>> alist ['a', 'b', 'c', 'd'] >>> blist = alist >>> id(alist) 139953038621824 >>> id(blist) 139953038621824
What is a tuple?
An ordered collection of elements that can't be modified. Indicated by parentheses. Tuples are useful for elements that shouldn't change during your program. >>> atuple = ('a', 'b', 'c') >>> atuple ('a', 'b', 'c') >>> atuple[0] = 'zed' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
How do you modify an element in a list?
Elements in a list are modified by specifying its index in square brackets. >>> alist ['a', 'b', 'c'] >>> alist[0] = 'B' >>> alist ['B', 'b', 'c']
How do you define a list?
Lists are created using square brackets: alist = ['a', 'b', 'c']
What is a for loop?
Loops that repeats a select number of times for x in alist: print(x) iterable objects (like list, tuples, str) can be specified in a for loop as these objects are capable of returning its members one at a time. In the above example, each time the loop iterates alist, x will be assigned the value from that position in alist.
What is a slice?
Part of a list that begins with the element at the first index specified and stops with the element before the last index specified. >>> alist ['a', 'b', 'c', 'd'] >>> alist[0:2] ['a', 'b']
Using a slice, how do you return items at the end of a list?
Use negative numbers to traverse the list backwards. As [-1] will return the last item in the list. ['a', 'b', 'c', 'd'] >>> alist[-1] 'd'
How do you add items to a list?
append(element) Adds the element to the end of the list. insert(index, element) Inserts the element at the provided index.
How can you create a copy of a list?
copy() function. Or using slicing with no indexes specified. >>> alist ['a', 'b', 'c', 'd'] >>> blist = alist.copy() >>> clist = alist[:] >>> blist ['a', 'b', 'c', 'd'] >>> clist ['a', 'b', 'c', 'd'] >>> id(alist) 139953038621824 >>> id(blist) 139953038621888 >>> id(clist) 139953038622016
How do you remove a specific element at an index in a list?
del deletes an item >>> alist ['b', 'c'] >>> del alist[0] >>> alist ['c']
How do you covert a variable to a list?
list(variable) >>> astring = 'abcd' >>> list(astring) ['a', 'b', 'c', 'd']
How do you remove and return an item from a list?
pop(index) Removes an element at the specified index (or end of the list if no index provided) and returns the elements value. >>> alist.pop(1) 'b' >>> alist ['a', 'c', 'd']
How do you remove a specific element from a list, without using its index?
remove(element) Removes a specific element from a list. >>> alist ['B', 'b', 'c'] >>> alist.remove('B') >>> alist ['b', 'c']
How do you sort a list permanently?
sort() function. Sorts and updates the current list. >>> nums [5, 3, 2, 4, 1] >>> nums.sort() >>> nums [1, 2, 3, 4, 5]
How do you sort a list temporarily?
sorted(variable) Returns a sorted list. >>> sorted(nums) [1, 2, 3, 4, 5] >>> nums [5, 3, 2, 4, 1]
How do you reverse a list?
sorted(variable, reverse=True) or variable.reverse()