Python: Lists

Python lists are data structures containing a group of elements within a single variable. Elements are access by offset (position), slice (offset range), or iteration. The first element is positioned at “index 0”. You can loop through a list with a “for” and “in” construct. You can also check for an element by value with an “if” and “in” construct.

List Methods

MethodDescription
.append()Adds an element to the end of the list
.clear()Empties the list
.copy()Returns a copy of the list
.count()Returns a count from the specified value
.extend()Adds an iterable to the end of the current list
.index()Returns the index of the first element from a specified value
.insert()Adds an element to a specified position
.pop()Removes an element at a specified position
.remove()Removes the first element with a specified value
.reverse()Reverses the order of a list
.sort()Sorts a list

List Examples

# Create an empty list with empty brackets.
empty_list = []
print(empty_list)
# Create an empty list with list() function.
empty_list = list()
print(empty_list)
# Create a list from a string (convert string to list).
list_from_string = list('python')
print(list_from_string)
# Create a list from a tuple (convert tuple to list).
our_tuple = ('item 1', 'item 2', 'item 3')
list_from_tuple = list(our_tuple)
print(list_from_tuple)
# Select an element with offset.
our_list = ['item 1', 'item 2', 'item 3']
print(our_list[0])
# Change (swap) an element with offset.
our_list = ['item 1', 'item 2', 'item 3a']
our_list[2] = 'item 3b'
print(our_list)
# Select a slice (range of elements) with an offset range.
our_list = ['item 1', 'item 2', 'item 3']
print(our_list[::2])
# Combine (merge) lists with extend.
our_list = ['item 1', 'item 2', 'item 3']
new_list = ['item 4', 'item 5']
our_list.extend(new_list)
print(our_list)
# Combine lists with "+=".
our_list = ['item 1', 'item 2', 'item 3']
new_list = ['item 4', 'item 5']
our_list += new_list
print(our_list)
# Add an element with .append()
our_list = ['item 1', 'item 2', 'item 3']
our_list.append('item 4')
print(our_list)
# Remove an element with offset with del.
our_list = ['item 1', 'item 2', 'item 3']
del our_list[1]
print(our_list)
# Remove an element by value with remove().
our_list = ['item 1', 'item 2', 'item 3']
our_list.remove('item 3')
print(our_list)
# Return and remove by specified offset with pop().
our_list = ['item 1', 'item 2', 'item 3']
element = our_list.pop(1)
print(element)
# Empty a list with clear().
our_list = ['item 1', 'item 2', 'item 3']
our_list.clear()
print(our_list)
# Copy a list.
our_list = ['item 1', 'item 2', 'item 3']
new_list = our_list.copy()
print(new_list)
# Return a count of a specified element in list.
our_list = ['item 1', 'item 1', 'item 3']
print(our_list.count('item 1'))
# Return the index of a specified element.
our_list = ['item 1', 'item 2', 'item 3']
print(our_list.index('item 1'))
# Reverse the elements of a list.
our_list = ['item 1', 'item 2', 'item 3']
our_list.reverse()
print(our_list)
# Sort the elements of a list.
our_list = ['item 1', 'item 2', 'item 3']
our_list.reverse()
our_list.sort()
print(our_list)
# Find (check) value in list with "if" and "in" construct.
our_list = ['item 1', 'item 2', 'item 3']
if 'item 1' in our_list:
print("Found")
# Loop through list with "for" and "in" construct.
our_list = ['item 1', 'item 2', 'item 3']
for element in our_list:
print(element)

About the author

Shane Bellone

At the intersection of capricious and whimsy.

Add comment

By Shane Bellone