by: Ryan Ernst
As someone who frequently switches between programming languages, I have developed a mostly-useless-super-power. I have gained the ability to forget basic syntax and built-in methods within days; more importantly, I can find answers to most syntax questions in less than 30 seconds. I have a suspicion that google-fu is a skill that all experienced developers gain. If you ever need to quickly google something esoteric, find your local software engineer.
When I have been away from Python for a few days, I often need to look up how to find the length of a list. This post is an effort to make this knowledge more sticky in my own brain and hopefully provide a quick answer for others who share this plight. I also share the most-common sequence operations that I think everyone should know (or at least know how to find easily). A sequence in python includes lists, tuples, and ranges. This article will focus only on lists, but these functions will also work on tuples and ranges as well!
Length of List (or any Sequence)
To find the length of a list, use the built-in len function. This function is built-in to Python and never needs to be imported. This should be the only way you find the length of a list. It is a built-in function for a reason.
Test If an Item Exists (or Not) in a Sequence
a = [1, 2, 3] b = ['a', 'b', 'c'] # Using in print(1 in a) # True print(1 in b) # False print(4 in a) # False print('c' in b) # True # Using not in print('c' not in b) # False print(143 not in a) # True
To test if an item is in a sequence, just use the
in keyword. To invert the result, you can use the
Find the Smallest or Largest Items in a Sequence
a = [0, 1, 2] b = ['alfred', 'ben', 'chris'] r = range(1000) print(min(a)) # 0 print(max(a)) # 2 print(min(b)) # alfred print(max(b)) # chris print(min(r)) # 0 print(max(r)) # 999
To find the smallest or largest items in a sequence, you can use the built-in
Find the First Occurrence of an Item in a Sequence
a = [0, 1, 2] b = ['alfred', 'ben', 'chris'] r = range(1000) print(a.index(1)) # 1 print(b.index('alfred')) # 0 print(r.index(252)) # 252 print(b.index('bob')) # ValueError: 'bob' is not in list
To find the first occurrence of an item in a sequence, you can use
s.index(<value>) where s is the sequence you want to search. Note that index will throw a ValueError if the item you are looking for is not in the sequence. Index also accepts extra arguments that let you specify the subsection of the sequence you want to search by passing in
before_index arguments. The full signature is:
s.index(x, after_index, before_index).
Count the Number of Times an Item is in a Sequence
a = [1, 2, 2, 2, 3] b = ['a', 'b', 'b', 'c'] print(a.count(2)) # 3 print(a.count(3)) # 1 print(a.count(0)) # 0 print(b.count('a')) # 1 print(b.count('b')) # 2 print(b.count('x')) # 0
To count the number of times an item is in a sequence, you can use
s.count(<value>) where s is the sequence you want to search.
I hope you have found this short post helpful! If you have any other python topics you would like to hear more about, or maybe you have a python problem you could use some help with, shoot us an email at firstname.lastname@example.org
P.S.: If you ever struggle with Python Imports, check out our other post on how to solve common import errors: