Create a list - Tom Kleen
Python Lists Quick GuideList: A?list?is a sequential collection of Python data values, where each value is identified by an index. The values that make up a list are called its?elements. Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can have any type and for any one list, the items can be of different types.Create a listalist = []alist = [1, 2, 3]Access a list elementalist[0]# first element is #0alist[-1]# last element is at -1, next-to-last is -2, etc.Get a slice of a lisalist[2:5]# elements 2, 3, & 4 (does NOT include element #5)alist[:5]# elements 0 through 4alist[5:]# element #5 through end of the listGet the length of a listlen(alist)Sum the elements in a list (if all numeric)sum(alist)Find largest or smallest item in a listmin(alistmax(alist)Concatenate two listsclist =alist + blist#Combines two lists, creating a third list.Append an element to a listalist.append(999)#Adds an element to a list. Changes alistalist> += [999]#Does same as append]Insert at position 5alist.insert(5, 99) # Insert 99 at pos 5. All other items move down.Remove from high endalist.pop()# Removes last item (highest-numbered location)Remove from a specific locationalist.pop(5)# Removes item at position 5 (6th element)Remove a specific item from a listalist.remove(99)# Removes first occurrence of 99 or raises ValueError.Replace a list elementalist[5] = 100# Changes item at position 5 to 100Delete a list elementdel alist[5]# Deletes item at position 5Sortingalist.sort()# sorts alist in ascending order. Changes alist.alist.sort(reverse=True)# sorts alist in descending order. Changes alist.sortedalist = sorted(alist)# does not change alistReversing a listalist.reverse() # Changes alistreversed = alist.reversed# Does not change alist, but creates a copyFind an itemalist.find(99)# Returns the position of 99. -1 if not foundalist.index(99)# returns the position of 99. Raises ValueError if not found.Count items on a listalist.count(99)# Return number of times 99 is on the listConverting a string to a list of characterschar_list = list("Hello")# sets char_list to ['H', 'e', 'l', 'l', 'o']Converting a dictionary to a list of key-value pairsdict_list = list({("Bob", 95), ("Chuck", 90)})# [("Bob", 95), ("Chuck", 90)]Clear a listalist = []alist.clear() ................
................
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
- activity sheet 11 1d arrays lists in python
- python programming project university of south alabama
- python part ii analyzing patient data
- d y patil college of engineering akurdi programming and
- chapter 1 scalar variables and data types
- programming exercise 3 1
- python part iv storing multiple values in lists
- create a list tom kleen
Related searches
- create a price list free
- create nested list python
- how to create a list in java
- a list b list c list celebrities
- create linked list in java
- create a linked list in java
- how to create a list in python
- create a link in a text file
- create a list in vba
- create a list in python
- create a new list in sharepoint
- create a list of lists python