Python Lists
[Pages:6]1/31/22
CS 224 Introduction to Python
Lists
1
Python Lists
Python's version of an array but some important differences: ? lists do not have a fixed size ? lists are heterogeneous ? can insert delete at arbitrary positions within list
2
1
1/31/22
Creating a List
my_list = [] my_list = list() my_list = [1, 2, 3]
list2 = my_list
# creates an empty list # creates an empty list # creates a list containing # elements 1, 2 and 3 # creates a reference to my_list
my_list = [1, 3.14, `test', [`a', `b']] # note different types
s = `abc' my_list = list(s)
# creates list [`a', `b', `c']
3
Accessor
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
nums[5] nums[len(nums)-1] nums[-1] nums[-2]
# value is 6 # value is 10 # value is 10 # value is 9
Accessor results in an element
4
2
1/31/22
Slices
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
nums[3:6] t = nums[4:] u = nums[:7] v = nums[-4:] w = nums[:-5]
# value is [4, 5, 6] # t is [5, 6, 7, 8, 9, 10] # u is [1, 2, 3, 4, 5, 6, 7] # v is [7, 8, 9, 10] # w is [1, 2, 3, 4, 5]
Slices are lists
5
List Operations
? len(my_list) ? min(my_list) ? max(my_list) ? sum(my_list) ? all(my_list) ? any(my_list)
# number of elements # minimum value # maximum value # sum of the values # True if all elements True # True if any element True
6
3
1/31/22
More List Operations
? my_list[i] = x ? my_list[i:j] = u_list
? del my_list[i] ? del my_list[i:j]
# element assignment # replaces slice with
# another list # delete ith element # delete slice
? u_list + v_list ? e in my_list
# concatenate # True if e is element in my_list
7
Methods to add to a list
my_list.append(e) adds element e to end of my_list
my_list.extend(u_list) concatenates u_list onto my_list
my_list.insert(i, e) inserts element e at position i
8
4
1/31/22
Methods to remove from a list
my_list.pop([i]) returns element at position i and removes it from my_list. if no parameter given, returns and removes last element.
my_list.remove(e) searches for e in my_list and removes if found. if multiple occurrences, removes only one.
9
Methods to rearrange a list
my_list.reverse() reverses elements of my_list in place.
my_list.sort() sorts elements of my_list in place.
my_list.sort([key, [, reverse]]) sorts elements of my_list in place. key is a sort-key function. reverse is an optional Boolean flag.
How is reverse different from sort with reverse flag?
10
5
1/31/22
Other list methods
my_list.count(e) returns number of occurrences of e in my_list.
my_list.index(e [, start [, stop]]) returns the smallest index i for which my_list[i] == e. optional parameters start and stop constrain the search.
sorted(my_list [, key [, reverse]]) returns a sorted version of my_list but does not change my_list.
11
6
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- python tutorial stanford computer vision lab
- working with lists and dictionaries ncert
- python tutorial list and negative indexing
- on to python
- python lists
- python programming 2 regular expressions lists
- cse 142 python slides university of texas at austin
- python day 3 lists branching iwks
- a tiny bit more python
- cs177 python programming purdue university
Related searches
- list of lists python example
- python add lists of lists
- python printing lists in columns
- python list of lists example
- python 3 lists tutorials
- nested lists python 3
- python replace lists value
- which of the following lists python units of abstraction from smallest to larges
- python lists are immutable
- python lists of lists format
- python list of lists indexing
- python sort list of lists by element