Go to Chapter 16 of A Smarter Way.
Return to our lists page:
Change an item in a list:
friends = ['bill', 'jeff', 'jim'] friends = 'fred' print(friends)
Add an item to a list:
friends.append("joel") # Note that we use parens, not brackets! # That's because this is a function. print(friends)
Find the index of an element:
Is an item in a list?
print('joel' in friends) print('fanny' in friends)
If an item isn’t in the list, in the example above, of course nothing prints.
The Critical Difference Between sorted( ) and sort( )
…and once again, scroll up because the page TOC is faulty.
sort( ) actually reorders the values in the list. The list, in other words, is changed.
sorted( ) does NOT change the list, it just returns a sorted version of the list to you.
- Continue editing examples.py.
- Change the value of one item in your list.
- Find the index of one item by name.
- Perform a test to see if an item is in your list.