Having the right tools makes any job significantly easier. This is just as true for writing computer programs in Python as it is for building hardware with your own two hands. A huge part of learning to write programs in a new language is learning about the tools that are available, so you can use them to make your life (and the lives of your collaborators) easier.

Fortunately, Python has many of these tools readily available for your use! I’ll introduce four of those tools and give you example code you can use to start implementing these tools in your own programs right away.

4 Key Python Tools

  1. enumerate
  2. zip
  3. sorted
  4. reversed

All four of these tools are designed to rapidly and conveniently manipulate sequences of data to make them more useful in your scripts.

Looking for More Control Flow?Learn the Fundamentals of Control Flow in Python

 

1. enumerate()

Enumerate is a particularly common tool to combine with your control flow processes, particularly the for loop. You’ll often need to keep track of the index currently in use because it’s supporting the calculations, necessary for calculations, or maybe it’s printing the current status to the terminal. Regardless of the reason, enumerating is a useful tool.

Historically, programmers iterate by creating an iterator, commonly named i, initializing it to zero, and adding one to it each time the code  executes. For instance, you might use the following code:

Data = [18, 24,5, 17, 21, 19]
i = 0
for value in Data:
   print(i, value)
   i += 1

This code performs the following steps:

  • First, it creates a list of values and stores them in the Data list.

  • Then it creates the iterator named i and initializes it to zero.

  • Third it starts a for loop that cycles through each value in the Data list.

  • Within that list it prints both the current value of i and the current value in Data.

  • Finally, it adds one to i indicating that the next cycle will use the next value in Data.

Now, this is how I iterated when I first learned computer programming in FORTRAN. I wish I had known better ways at the time! Fortunately the enumerate command in Python streamlines the process for us.

Enumerate returns both an iterator and a value from a data set. The iterator automatically tracks the index used in this pass through the for loop, thus fulfilling the same purpose as i in the above code.

Here’s how it works:

Data = [18, 24,5, 17, 21, 19]
for i, value in enumerate(Data):
   print(i, value)

With each pass through the for loop enumerate returns both the index and the considered value. In this way, the code returns the same value from Data and i as the but in half as many lines of code, while using a simple function that most of your collaborators will find familiar.

 

2. zip()

Zip creates a number of tuples out of a collection of lists, series, tuples or other sequences passed to it. It does so by combining all entries with the same index across all of the collections to create a tuple of all items with that index. Then the code moves on to the next index and creates a tuple for that index (and the next index, and the next…).

Let’s take a look at an example:

List_1 = [1,2,3,4,5]
List_2 = [6,7,8,9,10]
List_3 = [11,12,13,14,15]
zipped = zip(List_1, List_2, List_3)
print(zipped)

This code creates a new list named zipped from the three lists that we provide it. The code does this by combining all items from the first index to create a tuple, then combining all the items from the second index to create a tuple, and so on until it’s done for all five indices. The result is a list of tuples. 

[(1, 6, 11), (2, 7, 12), (3, 8, 13), (4, 9, 14), (5, 10, 15)]

  • The first tuple in the zipped list is (1, 6, 11). These numbers represent the first index from List_1, the first index from List_2 and the first index from List_3.
  • The second tuple is (2, 7, 12) which represents the second index from the three lists.
  • The third tuple is (3, 8, 13) and I bet you know where those numbers came from.

One of the main uses of zipped is to simultaneously iterate over multiple sequences, allowing your code to accomplish as much with a single for loop as it would with two for loops. You can combine this  with enumerate to keep track of the index you’re handling. Consider the following example:

for i, (a, b, c) in enumerate(zipped):
   print('{}: {}, {}, {}'.format(i, a, b, c))

This code will print a line of output for each index in the zipped list. It will print the index, followed by the value in each of the three lists corresponding to that index. Using our zipped variable from before, it returns:

0: 1, 6, 11

1: 2, 7, 12

2: 3, 8, 13

3: 4, 9, 14

4: 5, 10, 15

Looking for More on Python Lists?How to Append Lists in Python

 

3. sorted()

Sometimes you want to examine a list in a sorted order. Maybe you have a list of names and need them in alphabetical order. Or maybe you have numbers and need to see them sorted from smallest to largest. This isn’t necessarily an easy thing to do on your own. You could write a function to identify the first letter of each name in your list, compare it to the other names in your list and place it in the correct order, but that isn’t exactly clean or efficient.

Fortunately, sorted does exactly that and it is both clean and efficient. It does exactly what it says: this tool returns a sorted version of the list that you provide.

Let’s look at an example using the same Data list as before:

Data = [18, 24,5, 17, 21, 19]

Data_Sorted = sorted(Data)

If you print Data_Sorted you’ll find it yields [5, 17, 18, 19, 21, 24], a version of Data sorted from smallest to largest.

 

4. reversed()

Reverse is a useful tool that works well in combination with sorted because it lets you sort things in the opposite order. What if you have the same lists as before, but you want the names sorted in reverse alphabetical order? Or what if you want your numbers sorted from largest to smallest?

You use reversed.

First, you need to use sorted() to sort the list. Then you call reversed on that list to return it in the reversed order.

One thing to keep in mind with reversed is that it returns an iterable object, not an actual list. So if you want to treat it as a list you also need to use the list() function to convert it.

To create the reversed list of sorted data, start with Data_Sorted from the last example. Then: 

Data_Sorted_Reversed = list(reversed(Data_Sorted))

If you run that code and print the output you’ll find that it has returned [24, 21, 19, 18, 17, 5], which is the original Data list sorted from largest to smallest.

* * *

And there you have it! With the use of the enumerate(), zip(), sorted(), and reversed() tools your life (and the lives of your collaborators) will be easier in no time.

Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us