Python Forum
How can I print the number of unique elements in a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I print the number of unique elements in a list?
#1
Using the dictionary below, I'm supposed to create a function that returns the number of unique items in the "items" list.

shopping_cart = {
    "tax": .08,
    "items": [
        {
            "title": "orange juice",
            "price": 3.99,
            "quantity": 1
        },
        {
            "title": "rice",
            "price": 1.99,
            "quantity": 3
        },
        {
            "title": "beans",
            "price": 0.99,
            "quantity": 3
        },
        {
            "title": "chili sauce",
            "price": 2.99,
            "quantity": 1
        },
        {
            "title": "chocolate",
            "price": 0.75,
            "quantity": 9
        }
    ]
}
I've created the code below which outputs the number of items (5), but I need the code to only count unique items. For example, if two items in this list were identical, it should return 4 (since only 4 items would be unique in that case). How can I accomplish this?

def get_tax_rate(d):
    print (d['tax'])
Output:
5
Reply
#2
That function doesn't do what you claim it does - it only prints the value associated with the key "tax" in the dict.

In any case, how have you thought about solving the problem and what have you tried? We aren't going to do the work for you.
Reply
#3
(Mar-20-2020, 04:39 AM)ndc85430 Wrote: That function doesn't do what you claim it does - it only prints the value associated with the key "tax" in the dict.
and it's not 5 as you show in output, but 0.08
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Mar-19-2020, 10:53 PM)AnOddGirl Wrote:

Good morning,

Well as all above already mentioned, is your output not that what it should be / expect from your code :)
with
print(d['tax']
You only get the the value from the value "tax" in your dict.

Here is a short example how you can access/work through a nested-dict !
config = {
    "username": "user",
    "password": "password",
    "server": "https://website.de",
    "headers": {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:44.0) Gecko/20100101 Firefox/44.0",
        "Connection": "keep-alive"
    }
}

print(config['headers']['User-Agent'])
Will output:
Output:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:44.0) Gecko/20100101 Firefox/44.0
You can count all items(key/value pairs) from a dict with the len() function
Here (How to find length of dictionary values) you have a pretty decent example, which is actually part of that what you need.



Now you only need to call your created function the right way ( with the correct parameter " d " )


All the best :)
Tried to explain it as easy as I could in the morning without giving you the direct solution ...

That are at least the basics. The rest with the unique part is up to you :)
Shouldnt be that hard.
Reply
#5
You get the items, then you put the titles into a set or a set-comprehension, then get the len of the elements.

A set holds only unique elements without keeping the order.
A set does not allow mutable objects like dicts, so you have to use a unique feature from your item-dict.

Read what a set is and what it does.
len({item["title"] for item in shopping_cart["items"]})
If the value of title is not unique, you've a big problem.
The price or quantity is not a unique feature.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
I'm sorry for the mixup. Below is the code I've written that should have been in my OP (the incorrect code is from another problem).

def number_of_item_types(d):
    return (len(d['items']))
Thank you for all of your help. I understand now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,348 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  help with adding duplicates elements together in a list 2ECC3O 5 1,970 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  list digit into number Voldyy 2 1,493 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  Displaying list correspond to the column number danlopek14q 9 3,896 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  How to convert every even number in a list to odd? Bruizeh 4 3,666 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  Unexpected behavior accessing list elements. tonyflute 2 2,221 Apr-09-2021, 02:36 PM
Last Post: tonyflute
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,254 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  How to print all iterations of min and max for a 2D list alaina 4 2,831 Nov-11-2020, 05:53 AM
Last Post: alaina
  Why does this function print empty list? hhydration 1 1,501 Oct-28-2020, 02:03 AM
Last Post: deanhystad
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,279 Sep-24-2020, 02:26 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020