fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • set

    Imagine you have many customers from different cities across the world. You keep track of their cities and have a list of cities from where your customers come from. That list might be like this: ['San Francisco', 'New York', 'Yerevan', 'New York', 'Tel Aviv', ...].
    You have noticed there are some customers from the same city as New York. You would like to know which are the unique cities that you had customers from.
    This is easy to do with a set in Python:
    cities = ['San Francisco', 'New York', 'Yerevan', 'New York', 'Tel Aviv', 
    					'San Diego', 'San Francisco', 'Yerevan']
    uniques = set(cities)
    print(uniques)
    # {'San Diego', 'Yerevan', 'San Francisco', 'New York', 'Tel Aviv'}
    
    print(len(cities))     # 8
    print(len(uniques))    # 5
    A set is an unordered collection with no duplicate elements. It helps to keep track of the unique elements and has no particular order. It’s just a collection of elements similar to a list or a tuple but without order and guarantees the uniqueness of elements.
     
    We can check if an item is in the set with the in operator:
    print('San Francisco' in uniques)   # True
    print('Tel Aviv' in uniques)        # True
    print('London' in uniques)          # False
    print('Amsterdam' in uniques)       # False
    print(3 in uniques)                 # False
    It’s way faster to check if a certain element is present in a set compared to checking it in a list. This is due to the nature of the set as a data structure. For lists, Python needs to compare the elements one by one from the first element in the list to the last and return True if it was present and False otherwise. So, it iterates through the whole list one by one to do the check. In contrast, the structure of a set allows doing this check with way fewer operations.

    Challenge

    You are helping a grocery store to manage its inventory. For each product, they have written down the name of the company that delivers that product. They would like to send a message to those companies telling them how much of their goods they would need in the coming month. Yet, some companies deliver several products, so the store wouldn’t like to send several messages to the same store. Instead, they would like to get the list of companies that they work with so that they would send the message to them afterward. They ask you to write a program to print out all the companies that they partner with.
    The first line of the input contains a single integer n - the number of products. The next n lines contain the company name for each product.
    The program should output the unique names of the companies the store partners with, each on a separate line.
    Input
    Output
    5 Nestle Mars Nestle Coca Cola Nestle
    Nestle Mars Coca Cola
    To check your solution you need to sign in
    Sign in to continue