Python List

Working with Lists in Python

li = []
# You can start with a prefilled list
other_li = [4, 5, 6]
 
# Add stuff to the end of a list with append
li.append(1)    # li is now [1]
li.append(2)    # li is now [1, 2]
li.append(4)    # li is now [1, 2, 4]
li.append(3)    # li is now [1, 2, 4, 3]
# Remove from the end with pop
li.pop()        # => 3 and li is now [1, 2, 4]
# Let's put it back
li.append(3)    # li is now [1, 2, 4, 3] again.
 
# Access a list like you would any array
li[0]   # => 1
# Look at the last element
li[-1]  # => 3
 
# Looking out of bounds is an IndexError
li[4]  # Raises an IndexError
 
# You can look at ranges with slice syntax.
# (It's a closed/open range for you mathy types.)
li[1:3]   # => [2, 4]
# Omit the beginning
li[2:]    # => [4, 3]
# Omit the end
li[:3]    # => [1, 2, 4]
# Select every second entry
li[::2]   # =>[1, 4]
# Return a reversed copy of the list
li[::-1]  # => [3, 4, 2, 1]
# Use any combination of these to make advanced slices
# li[start:end:step]
 
# Make a one layer deep copy using slices
li2 = li[:]  # => li2 = [1, 2, 4, 3] but (li2 is li) will result in false.
 
# Remove arbitrary elements from a list with "del"
del li[2]  # li is now [1, 2, 3]
 
# Remove first occurrence of a value
li.remove(2)  # li is now [1, 3]
li.remove(2)  # Raises a ValueError as 2 is not in the list
 
# Insert an element at a specific index
li.insert(1, 2)  # li is now [1, 2, 3] again
 
# Get the index of the first item found matching the argument
li.index(2)  # => 1
li.index(4)  # Raises a ValueError as 4 is not in the list
 
# You can add lists
# Note: values for li and for other_li are not modified.
li + other_li  # => [1, 2, 3, 4, 5, 6]
 
# Concatenate lists with "extend()"
li.extend(other_li)  # Now li is [1, 2, 3, 4, 5, 6]
 
# Check for existence in a list with "in"
1 in li  # => True
 
# Examine the length with "len()"
len(li)  # => 6

Leave a Reply

Your email address will not be published. Required fields are marked *