Python Forum
Need insight on dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need insight on dictionaries
#1
I am brand new to python and I've been set some work on dictionaries. I'd say I had a good go at it but it appears to be wrong. If you could explain where I went wrong and maybe provide the right answer that would be appreciated and I'd understand it a bit more.

def list_uniqueness(the_list):
    '''
    Return a dictionary with two key-value pairs:
      1. The key 'list_length' stores the length of the_list as its value.
      2. The key 'unique_items' stores the number of unique items in the_list as its value.
    Arguments
    the_list: A list
    Examples
    l = [1, 2, 2, 4]
    dictionary = list_uniqueness(l)
    print(dictionary['list_length']) # prints 4
    print(dictionary['unique_items']) # prints 3
    '''

    # ====================================
    # Do not change the code before this

    # CODE1: Write code that will create the required dictionary
      dictionary =

    # ====================================
    # Do not change the code after this

    return dictionary


if __name__ == '__main__':
    l = [1, 2, 2, 4]
    dictionary = list_uniqueness(l)
    print(dictionary['list_length'])
    print(dictionary['unique_items'])
My attempt at this challenge was:
    dictionary {list_length, unique_items}
    list_length = len(l)
    a_set = set(l)
    unique_items = len(a_set)
Reply
#2
Nice try. Not going to fall for that trick again. How about you actually putting in some effort.
buran likes this post
Reply
#3
You seem to not understand, as this is a simple assignment.
please read: https://www.w3schools.com/python/python_...naries.asp
Reply
#4
Put some effort in?, trying to learn something brand new such as coding is a little difficult in the pandemic when I am overwhelmed with it. I appreciate the moderator for at least pointing me in the right direction.
Reply
#5
(Jan-29-2021, 02:11 PM)Mystix Wrote: Put some effort in?, trying to learn something brand new such as coding is a little difficult in the pandemic when I am overwhelmed with it. I appreciate the moderator for at least pointing me in the right direction.

Sorry, but this is not a serious effort:
(Jan-29-2021, 01:32 PM)Mystix Wrote:
dictionary {list_length, unique_items}
list_length = len(l)
a_set = set(l)
unique_items = len(a_set)
And you must have some learning materials - textbook, course notes, etc., right?
Serafim likes this post
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
#6
I have a homework assignment that involves dictionaries. With distance learning it is a lot harder for me to copy my classmate's work so I am hoping you would do all the work for me. I have not read anything about dictionaries, but I think I remember that they use curly brackets.

At least that would be honest.

You obviously have internet access and there are tons of Python tutorials about dictionaries. There are also hundreds of posts on forums about how to count the number of unique elements in a list. Any tutorial or discussion of dictionaries is going to describe how items are added to dictionaries, and you didn't do anything even close to that.

This looks just like the 5 minute effort spent struggling on tuples. Take some of the teacher example, make no effort to understand, rearrange the parts and beg "Please help me". You are not going to learn anything that way. DeaD_EyE is a nice guy but he did you no favor. Spend some time on study this time and make a better effort. If you really try you will not need any help on this assignment. If you are given the answer to this assignment you will not be able to do the next.

In the really unlikely event that you cannot solve the problem yourself, even after putting in a few hours on it, then feel free to ask for help.
Larz60+, Serafim, buran like this post
Reply
#7
ya, I'm sure he is lovely, but making assumptions about me when he doesn't have a clue who I am. Just makes him sound like an old hermit complaining about his classmates begging him for his work 50 years ago. Like I said before I am new to coding and python in the middle of a pandemic at the comfort of my own home with no support being overwhelmed by this. As simple as it may to you guys it might be difficult for me to grasp and retain information. Also trying to proclaim that I'm a person who copies off classmates when I don't even attend school anymore shows me how much of an egoistic forum this is when I was hoping this would be a "nice" community.
Reply
#8
A dictionary is named the way it is, because it's very similar to an actual dictionary. You look up words, and get a definition. In python, the "key" is the word you're looking up in the dictionary, and the "value" is what was found there.

You can create them a few different ways. For a small one, like this, doing it all in one line is fine:
things = {"list_length": 2342, "unique_items": [38, 23, 0]}
You can also create an empty one, and add items:
things = {}
things['list_length'] = 2342
things['unique_items'] = [38, 23, 0]
There's a couple other ways, but they're not relevant for this. Does that help?
Reply


Forum Jump:

User Panel Messages

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