SEE ALSO: Python Lists in 5 Minutes or Less .. Part 1
Extracting Part of a List (sublist)
Lists can be sliced using [:]
construction. list[start:end]
(slicing) returns a new list containing elements in the original list from index position "start" to "(end-1)". Index starts at 0. "start" and "end" positions are optional. When "start" position was omitted, start of the list is assumed. When "end" position was omitted, end of the list is assumed. If both are omitted, entire list is returned as is.
>>> record ['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, '925-93-2176', 'John', 'Keats', 'Sears Tower, Chicago, IL 46371'] >>> record[4:7] [140.32, '925-93-2176', 'John'] >>> record[:4] ['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201'] >>> record[4:] [140.32, '925-93-2176', 'John', 'Keats', 'Sears Tower, Chicago, IL 46371'] >>> record[:] ['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, '925-93-2176', 'John', 'Keats', 'Sears Tower, Chicago, IL 46371']
Negative index counts back from the end of the list (right-to-left). -1 refers to the right most (last) element in the list.
eg.,Extract last 3 elements in the list using negative index. >>> record[-3:] ['John', 'Keats', 'Sears Tower, Chicago, IL 46371'] Extract all elements except the last 3. >>> record[:-3] ['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, '925-93-2176']
An optional "step" parameter can be specified to skip few elements from the list, or put another way to get every nth element from the list. Syntax: list[start:end:step]
Fetch every alternate element from the beginning of the list. >>> record[::2] ['Gary', 25, 140.32, 'John', 'Sears Tower, Chicago, IL 46371'] Reverse the list. >>> record[::-1] ['Sears Tower, Chicago, IL 46371', 'Keats', 'John', '925-93-2176', 140.32, 'Network Ct, Twin Peaks, WA 90201', 25, 'Doe', 'Gary']
Note that the new lists returned from slicing a list are in reality shallow copies so contain references to elements from the original list.
Sorting a List
sort()
method of the list object returns nothing (None
) but modifies the original list by sorting in place. sort
method supports 3 optional parameters -- method to be used for sorting (cmp), function to be executed with key element (key) and the reverse
option. Syntax: sort(cmp, key, reserve)
.
>>> record ['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, '925-93-2176', 'John', 'Keats', 'Sears Tower, Chicago, IL 46371'] >>> record.sort() >>> record [25, 140.32, '925-93-2176', 'Doe', 'Gary', 'John', 'Keats', 'Network Ct, Twin Peaks, WA 90201', 'Sears Tower, Chicago, IL 46371'] >>> record=['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, '925-93-2176', 'John', 'Keats', 'Sears Tower, Chicago, IL 46371'] >>> record.sort(reverse=True) >>> record ['Sears Tower, Chicago, IL 46371', 'Network Ct, Twin Peaks, WA 90201', 'Keats', 'John', 'Gary', 'Doe', '925-93-2176', 140.32, 25]
If you don't want the original list to be modified, one option is to make a copy of the list and call sort()
on the copy. Another option is to use the built-in sorted()
function. The sorted()
function returns the sorted list. It also accepts reverse
parameter similar to sort()
of the list.
>>> record=['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, '925-93-2176', 'John', 'Keats', 'Sears Tower, Chicago, IL 46371'] >>> import copy >>> recorddup = copy.copy(record) >>> recorddup.sort() >>> recorddup [25, 140.32, '925-93-2176', 'Doe', 'Gary', 'John', 'Keats', 'Network Ct, Twin Peaks, WA 90201', 'Sears Tower, Chicago, IL 46371'] >>> record ['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, '925-93-2176', 'John', 'Keats', 'Sears Tower, Chicago, IL 46371'] >>> sorted(record) [25, 140.32, '925-93-2176', 'Doe', 'Gary', 'John', 'Keats', 'Network Ct, Twin Peaks, WA 90201', 'Sears Tower, Chicago, IL 46371'] >>> sorted(record, reverse=True) ['Sears Tower, Chicago, IL 46371', 'Network Ct, Twin Peaks, WA 90201', 'Keats', 'John', 'Gary', 'Doe', '925-93-2176', 140.32, 25]
Comparing Lists
Lists can be compared for equality using ==
operator. Lexicographical comparison is possible with the help of less-than and greater-than operators.
>>> record ['Gary', 'Doe', 25, 'Network Ct, Twin Peaks, WA 90201', 140.32, '925-93-2176', 'John', 'Keats', 'Sears Tower, Chicago, IL 46371'] >>> recorddup [25, 140.32, '925-93-2176', 'Doe', 'Gary', 'John', 'Keats', 'Network Ct, Twin Peaks, WA 90201', 'Sears Tower, Chicago, IL 46371'] >>> record == recorddup False >>> sorted(record) == recorddup True >>> record > recorddup True >>> record < recorddup False
FYI
ReplyDeleteTo clone/copy a list, I prefer to using one line of code:
list_a = list_b[:]
To check if some item exists in a list and you don't care on which position this item appear in the list
if 3 in [1,2,3,4,5]:
print "get it"
This way runs faster than list method "index()" and when using "index()" method the programmer need captures the exception if the value is not in the list
Just two cents. ^_^