Introduction to data analysis with Python: Part 2 – Lists, Tuples and Dictionaries

Introduction to data analysis with Python: Part 2 - Lists, Tuples and Dictionaries




Introduction

For everything in life, there are building blocks, and to fully understand a concept, we usually have to go down to those basics.
Python has building blocks that we’re going to dive into in this article.



Lists

A list is an ordered, mutable collection of items. They can hold different data types.

Characteristics of Lists

  1. They are ordered.
  2. They can be changed (mutable).
  3. They can hold different data types.
  4. They can be indexed.
  5. They are stored in square brackets [ ].
  6. They accept duplicate elements.

Examples of lists

Indexing
Items in a list can be accessed using a method called indexing.
Indexes, in this context, are numbers assigned to the items in a list. The items are indexed/numbered starting from 0.

An example would be:

Image description

To fetch items, we write:

Positive indexing: This is simply calling items with their natural index numbers.

Image description

Negative indexing: Negative indexing works backward. If you want to pick the -4 item, you count from right to left starting from 1.
This could be useful when you have a long list and want to access the second-to-last item. For instance, writing -2 would do that.

Image description

Slicing indexing: Slicing allows us to pick a group of items. When slicing, you need two indexes—the index to start slicing from and the index to end at. However, the ending index is not included in the result.
As we see in the example below, print(a_list[1:4]) prints items at index 1 to 3. Index 4 (Tolu) is excluded.
We can also slice like this: print(a_list[:3]). This asks Python to return the values from the beginning up to index 2, as index 3 is excluded.

Image description

Skipping indexing: As the name implies, skipping indexing allows us to skip items we do not want. In a list of numbers, skip indexing can give us only even numbers.

Image description

Image description

Mutability of lists
As we’ve mentioned earlier, lists are mutable. We can update them and delete items from them.

Image description



Tuples

A tuple is an immutable and ordered collection of elements.

Characteristics of Tuples

  1. They are ordered.
  2. They are unchangeable (immutable).
  3. They can hold different data types.
  4. They can be indexed.
  5. They are stored in curved brackets ().
  6. They accept duplicate elements.
  7. They are faster than lists because they are immutable.

Example of Tuples

Image description

Tuples share the same indexing style as Lists.

Image description

Immutability of Tuples: They cannot be deleted or updated.

Image description



Dictionaries

A dictionary is an unordered, mutable collection of key-value pairs.

Characteristics of Dictionaries

  1. They are unordered.
  2. They can be changed (mutable).
  3. They can hold different data types.
  4. They are indexed not by position but by keys.
  5. They are stored in curly brackets {}.
  6. They accept duplicate value elements.
  7. Keys must be unique.

Example of a dictionary

Image description

To retrieve values, we use the keys. In this case, the keys are: name, age, gender, country, friend.

Image description

To update, you assign a new value using an equals sign (=).

Image description



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.