Python for Data Science — All about Lists

Aditya Daria
4 min readApr 17, 2022

--

Learning About Lists

Lists are the most commonly used data structure. Think of it as a sequence of data that is enclosed in square brackets and data are separated by a comma. Each of these data can be accessed by calling its index value.

  • A list is a mutable sequence that is typically a collection of homogeneous items. Mutable means that you can change a list after its creation.
  • You will frequently see lists that contain other lists. These are known as nested lists.
  • You will also see lists that contain all manner of other data types, such as dictionaries, tuples, and other objects.

In this chapter, you will learn the following:

  • Creating Lists
  • List Methods
  • List Slicing
  • List Copying

Let’s find out how you can create a list!

  • Using a pair of square brackets with nothing inside creates an empty list: []
  • Using square brackets with comma-separated items: [4,5,6]
  • Using a list comprehension (see Chapter 13 for more information): [x for x in iterable]
  • Using the list() function: list(iterable)

An iterable is a collection of items that can return its members one at a time; some iterables have an order (i.e. sequences), and some do not. Lists themselves are sequences. Strings are sequences as well. You can think of strings as a sequence of characters.

  1. Empty Lists

2. Numbered List

3. List of String Elements

4. Converting String to list

5. Mixed list with Heterogeneous elements

Access List

Lists are made to be worked with. You will need to learn how to access individual elements as well as how to change them.

  • Let’s start by learning how to access an item. Consider the following list
  • To access an item in a list, you need to use square braces and pass in the index of the item that you wish to access. In the example above, you access the first and third elements using the below.
  • Lists also support accessing items in reverse by using negative values.
  • If you try to use an index that does not exist in the list, you will get an Index Error.

List Slicing

Python lists support the idea of slicing.

  • Slicing a list is done by using square brackets and entering a start and stop value.
  • Syntax -> my_list[start:end]
  • For example, if you had my_list[2:6], you would be saying that you want to create a new list with the element starting at index 1 through index 6 but not including index 6.

Let's fetch the second and third elements from the list below :

You can also use negative values to slice. In this example, you didn’t specify an end value. That means you want to start at the second to last item in the list, 50, and take it to the end of the list.

Let’s try another example where you specify only the end index. In this example, you want to grab all the values starting at index 0 up to but not including index 3.

Extended Slicing

  • Extended slicing is similar to normal slicing but it takes a third parameter as a difference or steps to iterate that object after every step.
  • Step parameters should be a positive number if you want to read the list from beginning to end, and a negative number if you want to read a list from end to beginning
  • Syntax : list[start:end:step]

Let us fetch every 2nd and 3rd element from the list.

We will talk about list operations and methods in our next article.

I hope that you will find this article insightful. If you like to, please share the link with your friends, family, and colleagues.

Would you like me to encourage us to spread the insights on more topics, Please encourage us with a cup of coffee

For any suggestions/feedback please comment. Also please consider following and subscribe using my link.

--

--