Lists, Tuples, Slicing, Sets, and List Comprehensions with Python

2020-08-01 13:14:30 | #programming #python

After completing the prerequisites, you should have a good understanding of what a Python project is composed of. We will now go over some basic Python programming concepts so you can add some functionality to your programs.

In this guide, we're focusing more on some of the intermediate list capabilities of Python. We'll start with the basics, with list creation, appending, and retrieving by index. And then move on to more advanced concepts.

Lists

Lists give us the power to store data/values into a collection, reorder and modify them.

Manually Creating a List with Python

Manual list creation is as easy as definining comma-separated values within square brackets. By storing the list into the variable items, we can access its elements by index, starting at 0.

Try It Yourself


 

Creating a List with Python Dynamically Using Range

When we need to define a range of elements, we pass the starting value and ending value into the range function. The following example also demonstrates how to iterate through each item in the list with a for loop.

Try It Yourself


 

Appending to a List Using Python

The append function allows us to add elements to our list.

Try It Yourself


 

Negative Indices with Python

Negative indices allow us to retrieve an element from the end of a list. So if items is 10 elements long, items[-1] will return the last element of the list. items[-2] will return the second to last element of the list.

Try It Yourself


 

Extracting a Range of Elements from a Python List with Slice

Try It Yourself


 

Python Stride

Stride is how many steps to take across a range of indices. So providing a stride of 2 will iterate by 2 instead of 1. Check out the following examples to learn more:

Try It Yourself


 

Append vs. Extend

extend ends up being the better tool for merging lists together, since append inserts the set of values, which produces a nested list.

Try It Yourself


 

Pop vs. Remove

Use pop when you want to delete an element by index and remove when you want to delete an element by value. If you don't pass an index to pop, it will delete the element at the end of the list.

Try It Yourself


 

Tuples

Tuples are very similar to lists, but they're immutable. Once defined, you can't change the values, add to, or remove from a tuple. So what is the advantage of immutability? Immutable objects are inherently simpler and reduce the number of bugs in a program because the state of the object doesn't change and dependencies don't need to account for all the possible states.

Also, while lists have order, tuples have structure. Tuples are often found to store pairs or triplets of data, i.e. XY coordinates or RGB values. Note: you create tuples with parenthesis, rather than square brackets. Check out the examples below.

Try It Yourself


 

Knowing that tuples are immutable, what do you think would happen if you tried to append, pop, or index?

Advantages Over Lists

  • Tuples perform better than lists. Always use tuples when working with constant values you just need to iterate over.
  • Tuples require less memory than lists. Because you can append to lists, Python will actually create a larger allocation for lists in memory in case you append.
  • Tuples can be used as dictionary keys because they are immutable, where a list isn't.

Try It Yourself


 

List Comprehensions

List comprehensions are kind of a shorthand way of writing loops.

Performing Mathematical Operations on Every Element

Try It Yourself


 

Filtering Elements

Try It Yourself


 

Sets

What makes sets different is that they are unordered, and unindexed, and written with a different syntax.

Creating and Retrieving Values from a Python Set

Try It Yourself


 

Modifying a Python Set

Try It Yourself


 

Comments

You must log in to comment. Don't have an account? Sign up for free.

Subscribe to Our Newsletter

Would you like to receive free whitepapers and other IT news? Just leave your email address below. You may opt out at any time.



Tell Us About Your Project









Contact Us

Do you have a specific IT problem that needs solving or just have a general IT question? Use the contact form to get in touch with us and an IT professional will be with you, momentarily.

Hire Us

We offer web development, enterprise software development, QA & testing, google analytics, domains and hosting, databases, security, IT consulting, and other IT-related services.

Free IT Tutorials

Head over to our tutorials section to learn all about working with various IT solutions.

Contact