Programming Essentials Python - Overview of Collections - list and set - Usage

In this article, we will explore the usage of lists and sets in Python applications. We will learn how to perform various operations using lists and sets effectively.

Key Concepts Explanation

Reading data from file into a list

path = '/data/retail_db/orders/part-00000'

orders_file = open(path)
orders_raw = orders_file.read()
orders = orders_raw.splitlines()
orders[:10]
len(orders)

Get unique elements from the list

dates = ['2013-07-25 00:00:00.0', '2013-07-25 00:00:00.0', '2013-07-26 00:00:00.0', '2014-01-25 00:00:00.0']
dates
len(dates)
set(dates)
len(dates)

Creating new collection retaining duplicates using 2 sets

s1 = {'2013-07-25 00:00:00.0', '2013-07-26 00:00:00.0', '2014-01-25 00:00:00.0'}
s2 = {'2013-08-25 00:00:00.0', '2013-08-26 00:00:00.0', '2014-01-25 00:00:00.0'}
s1.union(s2)
len(s1.union(s2))
s = list(s1) + list(s2)
s
len(s)

Hands-On Tasks

Let’s put our knowledge into practice with the following tasks:

  1. Read data from a file into a list
  2. Get unique elements from a list
  3. Create a new collection retaining duplicates using sets

Conclusion

In this article, we have explored the key concepts of using lists and sets in Python programming. By practicing the hands-on tasks provided, you can strengthen your understanding of these fundamental data structures. Keep exploring and stay engaged with the community for further learning opportunities.

list and set - Usage

Note: This article includes commands and code examples that are demonstrated in the accompanying video. Check out the video for a visual walkthrough and further clarification.

[Embed the video here]

Remember, practice makes perfect!

Watch the video tutorial here