Creating Python Lists and Sets using Employees Data

This exercise is designed to enhance your understanding of Python collections, specifically lists and sets, and how they can be utilized in handling and processing data sets. By working with a realistic example of employee records, you will practice extracting specific information using Python’s powerful data structures and comprehensions or loops. The ability to efficiently create and manipulate lists and sets is crucial in data analysis, and this exercise provides a practical context to develop these skills.

Data Set:

You are provided with a list of 20 employee records. Each record is a Python dictionary containing several details about an employee, but you will focus on the employee ID. Note that there are some duplicate IDs within the data set.

Example of a single employee record:

employee_data = [
    {"employee_id": "E001", "name": "Alice", "position": "Developer", "department": "IT"},
    {"employee_id": "E002", "name": "Bob", "position": "Analyst", "department": "Business"},
    {"employee_id": "E003", "name": "Charlie", "position": "Manager", "department": "HR"},
    {"employee_id": "E004", "name": "David", "position": "Designer", "department": "Marketing"},
    {"employee_id": "E005", "name": "Eve", "position": "Engineer", "department": "Operations"},
    {"employee_id": "E006", "name": "Frank", "position": "Accountant", "department": "Finance"},
    {"employee_id": "E007", "name": "Grace", "position": "Administrator", "department": "Admin"},
    {"employee_id": "E008", "name": "Henry", "position": "Consultant", "department": "Strategy"},
    {"employee_id": "E009", "name": "Isabella", "position": "Advisor", "department": "Customer Service"},
    {"employee_id": "E010", "name": "Jack", "position": "Supervisor", "department": "Production"},
    {"employee_id": "E011", "name": "Karen", "position": "Technician", "department": "Quality Control"},
    {"employee_id": "E012", "name": "Leo", "position": "Executive", "department": "Leadership"},
    {"employee_id": "E013", "name": "Mia", "position": "Researcher", "department": "R&D"},
    {"employee_id": "E014", "name": "Noah", "position": "Planner", "department": "Logistics"},
    {"employee_id": "E015", "name": "Olivia", "position": "Trainer", "department": "Education"},
    {"employee_id": "E003", "name": "Charlie", "position": "Manager", "department": "HR"},
    {"employee_id": "E016", "name": "Pete", "position": "Officer", "department": "Security"},
    {"employee_id": "E017", "name": "Quinn", "position": "Clerk", "department": "Legal"},
    {"employee_id": "E002", "name": "Bob", "position": "Analyst", "department": "Business"},
    {"employee_id": "E014", "name": "Noah", "position": "Planner", "department": "Logistics"}
]

Problem Statement:

Your task is to create a Python list of employee IDs from the given data set. Then, you need to extract the unique employee IDs from this list to understand how many unique individuals are there in the dataset.

  1. Create a list named employee_ids that contains the IDs of all employees from the provided JSON data.
  2. Generate a set named unique_employee_ids that contains only unique employee IDs from the employee_ids list.
  3. Hint: Utilize loops or list comprehensions for these tasks.

Steps to Verify or Unit Test Cases:

To verify your solution, consider the following steps and corresponding unit test cases:

  1. Check if employee_ids list contains exactly 20 IDs, including duplicates.
  2. Validate if unique_employee_ids accurately reflects the unique IDs without any duplicates.
  3. Compare the lengths of employee_ids and unique_employee_ids to ensure duplicates were removed.

Example unit test case for checking the lengths:

def test_unique_ids():
    assert len(employee_ids) == 20, "The employee_ids list should contain 20 records."
    assert len(unique_employee_ids) == len(set(employee_ids)), "Unique IDs count mismatch."

Sample Output:

Assuming there were 3 duplicate IDs in the provided data set, the expected output would look like this:

Number of employees: 20
Number of unique employees: 17

Conclusion:

By completing this exercise, you have practiced how to manipulate and analyze data using Python lists and sets. These structures are fundamental yet powerful, enabling efficient data processing and analysis. Continue to explore more exercises on Python Collections to master these essential skills and apply them in various Python programming contexts.

Code Snippet:

Here’s a simple snippet demonstrating part of the possible solution:

# Creating a list of employee IDs
employee_ids = [employee['employee_id'] for employee in employee_data]

# Extracting unique employee IDs
unique_employee_ids = set(employee_ids)

Possible Solution:

Below is a possible solution, reflecting best practices in terms of readability, performance, and correctness.

# Sample employee data (a subset of the entire dataset)
employee_data = [
    {"employee_id": "E001", "name": "Alice", "position": "Developer", "department": "IT"},
    {"employee_id": "E002", "name": "Bob", "position": "Analyst", "department": "Business"},
    # Include more records as per provided dataset...
]

### Extracting employee IDs
employee_ids = [employee['employee_id'] for employee in employee_data]

### Obtaining unique employee IDs
unique_employee_ids = set(employee_ids)

# Displaying the results
print(f"Number of employees: {len(employee_ids)}")
print(f"Number of unique employees: {len(unique_employee_ids)}")

By iterating over various such exercises, you will deepen your understanding and ability to work efficiently with Python collections.

Remember, consistent practice is essential for becoming proficient in Python. Don’t hesitate to experiment with your Python code, explore different libraries, and tackle various challenges to enhance your understanding.


Thank you for exploring the Python category on ITVersity! If you are keen on diving deeper, engaging in discussions, seeking help, or sharing your Python knowledge, we encourage you to sign up if you haven’t yet. Joining our community gives you the opportunity to interact with other Python enthusiasts and experts. Your engagement and contributions will enrich our shared learning journey. Sign up today and become an active member of our dynamic community to discuss, learn, and advance together in the world of Python programming.