

The splat operator can be used to pass all of the contents of a list to a function. Let’s take a look at some other ways to print Python lists!Īnother way to print all of the contents of a list is to use the * or "splat" operator. Hopefully, you can see that Python provides some great tools for accessing sequences of information stored in lists. We use list indexing again to access the element at the location specified by element. The output here is the same, but instead of element referring to an actual element in our list as it did in the previous example, element now refers to the index position. You can also loop through a list using a for loop in conjunction with the built-in Python range() method: for element in range(len(my_list)): Imagine how much work this would save if your list had 50,000 elements instead of just three! Here, you can see that I was able to loop through each element of my list without having to specify each element manually. Output: Image 2 - Printing a Python list in a for loop (image by author) Let’s print some formatted strings based on the contents of our Python list! for element in my_list: You can write almost the same thing in Python to loop through your Python list. You might say something like, “For each item in my grocery list, I want to buy this quantity”. Imagine you want to describe your intention of buying the items on your grocery list to a friend. What makes this technique so intuitive is that it reads less like programming and more like written English! Perhaps one of the most intuitive ways to access and manipulate the information in a list is with a for loop. Next up, I am going to show you how you can start to leverage some of Python’s other built-in features to access information in lists much more efficiently. After all, we had to specify each element we wanted to print individually in the example above. Now, that’s certainly useful, but you might be wondering how any of this is more efficient than working with normal variables. Notice how I use the indices 0 and 2 to represent the first and third elements since Python indexing begins with 0. Output: Image 1 - Printing individual list elements in Python (image by author) For instance, if I want to print the first and third elements of my_list, I would use the following code: print(my_list, my_list) With list indexing, one simply puts the number (starting with 0) of the element one wishes to access in square braces following the name of the list. If I want to access any of this information specifically, I would use a technique called list indexing.

You might think about this list as representing the items on a grocery list and the quantities I want to buy. Here, I have defined a list called my_list whose elements strings, but the elements of a list can also be numbers like integers and floats, other lists, or a mix of different types.
PYTHON MAKE A LIST HOW TO
Now we’ll take a look at how to define lists and access the information within them. While array and list structures in some programming languages require learning funky syntax and have strict rules about what data types they can hold, Python lists have none of that! This makes them super simple to use and equally flexible. If a person wants to store a sequence of to-do’s so they’ll remember them throughout the day, they may write a to-do list. This is not unlike analog lists that you may be familiar with. More specifically, they store sequences of information.


Like variables, Python lists also store information. This is part of what makes computers such terrific tools we can feed them a bunch of information, and they can remember it and manipulate it way more easily than humans can. Variables allow the programmer to store information, like numbers or strings of text, and reuse it efficiently. One of the most important features of any programming language is the ability to define variables. I’ll cover the basics of printing a list using Python’s built-in print() method, printing a list using loops, as well as some neat formatting tricks such as printing a list on multiple lines.ĭon't feel like reading? Watch my video instead: I’ll be showing several different techniques for printing a list in Python. We are going to have a look at how anyone can leverage lists as an essential tool for automation and sailing through tedious tasks. Python’s list data structure is built for simplicity and flexibility. Explore endless possibilities of printing and formatting lists in Python
