Sitemap
Pinch of Code

Pinch of Code

Python programmer, web developer, YouTuber. You can visit my channel on youtube.com/@pinchofcode.

Python Lists: A complete guide

4 min readDec 25, 2022

--

Hello! Do you like making lists? In Python, we can make lists too! A list is just a collection of things that we want to keep track of, like a grocery list or a list of our favorite colors.

To make a list in Python, we put a bunch of things inside of square brackets [ ] and separate them with commas. For example, here’s a list of our favourite colours:

Creating a List:

favorite_colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']

We can also put numbers in our list, like this:

numbers = [1, 2, 3, 4, 5]

We can even mix different types of things in our list, like this:

mix = ['red', 2, 'orange', 4, 'yellow']

Accessing List Elements:

To access a specific item in our list, we use its index. The index is just the place where the item is in the list. We start counting at 0, so the first item in the list is at index 0, the second item is at index 1, and so on.

For example, if we want to get the first color in our favorite_colors list, we can do this:

first_color = favorite_colors[0]
print(first_color) # Output: 'red'

We can also use negative indices to start counting from the end of the list. The last item in the list is at index -1, the second to last item is at index -2, and so on.

For example, if we want to get the last color in our favorite_colors list, we can do this:

last_color = favorite_colors[-1]
print(last_color) # Output: 'purple'

We can also use slices to get a part of our list. A slice is just a part of the list that we specify using two indices. The first index is the starting point, and the second index is the ending point (but it doesn’t include the item at the ending point).

Get Pinch of Code’s stories in your inbox

Join Medium for free to get updates from this writer.

For example, if we want to get the first three colors in our favorite_colors list, we can do this:

first_three = favorite_colors[0:3]
print(first_three) # Output: ['red', 'orange', 'yellow']

We can also omit the first or second index to start at the beginning or end of the list, like this:

all_but_last = favorite_colors[:-1]
print(all_but_last) # Output: ['red', 'orange', 'yellow', 'green', 'blue']

all_colors = favorite_colors[:]
print(all_colors) # Output: ['red', 'orange', 'yellow', 'green', 'blue', 'purple']

Methods used with Lists:

ists in Python have several built-in methods that allow you to perform various operations on them. Here are a few examples of common list methods:

  • append(): adds a new item to the end of the list
colors = ['red', 'orange', 'yellow']
colors.append('green')
print(colors) # Output: ['red', 'orange', 'yellow', 'green']
  • extend(): adds multiple items to the end of the list
colors = ['red', 'orange', 'yellow']
colors.extend(['green', 'blue', 'purple'])
print(colors) # Output: ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
  • insert(): adds a new item at a specific position in the list
colors = ['red', 'orange', 'yellow']
colors.insert(1, 'green')
print(colors) # Output: ['red', 'green', 'orange', 'yellow']
  • remove(): removes the first occurrence of a specific item from the list
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
colors.remove('orange')
print(colors) # Output: ['red', 'yellow', 'green', 'blue', 'purple']
  • pop(): removes an item at a specific position in the list (or the last item if no position is specified) and returns it
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
last_color = colors.pop(-1)
print(last_color) # Output: 'purple'
print(colors) # Output: ['red', 'orange', 'yellow', 'green', 'blue']

second_color = colors.pop(1)
print(second_color) # Output: 'orange'
print(colors) # Output: ['red', 'yellow', 'green', 'blue']
  • index(): returns the index of the first occurrence of a specific item in the list
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
index = colors.index('yellow')
print(index) # Output: 2
  • count(): returns the number of occurrences of a specific item in the list
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'yellow']
count = colors.count('yellow')
print(count) # Output: 2
  • sort(): sorts the items in the list in ascending order
numbers = [5, 2, 7, 1, 3]
numbers.sort()
print(numbers) # Output: [1, 2, 3, 5, 7]
  • reverse(): reverses the order of the items in the list
numbers = [5, 2, 7, 1, 3]
numbers.reverse()
print(numbers) # Output: [3, 1, 7, 2, 5]

I hope these examples help you understand some of the common list methods in Python. Remember, you can use these methods to perform various operations on your lists, such as adding, removing, or searching for items, sorting or reversing the order of the items, and so on.

Here are a few more examples of list methods:

  • clear(): removes all items from the list
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
colors.clear()
print(colors) # Output: []
  • copy(): returns a copy of the list
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
new_colors = colors.copy()
print(new_colors) # Output: ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
  • min(): returns the smallest item in the list (for a list of numbers)
numbers = [5, 2, 7, 1, 3]
minimum = min(numbers)
print(minimum) # Output: 1
  • max(): returns the largest item in the list (for a list of numbers)
numbers = [5, 2, 7, 1, 3]
maximum = max(numbers)
print(maximum) # Output: 7
  • sum(): returns the sum of all items in the list (for a list of numbers)
numbers = [5, 2, 7, 1, 3]
total = sum(numbers)
print(total) # Output: 18

These are just a few examples of the many methods available for lists in Python. You can find more information about lists and their methods in the Python documentation (https://docs.python.org/3/tutorial/introduction.html#lists) or by doing some online research.

Conclusion:

In conclusion, lists are an essential data structure in Python that allow us to store and organize a collection of items. We can use various list methods to perform operations on our lists, such as adding, removing, or searching for items, sorting or reversing the order of the items, and so on. Whether you’re a beginner or an experienced programmer, learning how to use lists in Python is a valuable skill that can help you solve many different types of problems.

If you have any more questions about lists in Python, feel free to follow my Instagram page, https://www.instagram.com/pinchofcode/, where I share programming tips and resources for beginners. I hope you found this article helpful, and I wish you the best of luck on your programming journey!

--

--

Pinch of Code
Pinch of Code

Written by Pinch of Code

Python programmer, web developer, YouTuber. You can visit my channel on youtube.com/@pinchofcode.