Python Forum
get method not counting number of strings in dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get method not counting number of strings in dictionary
#1
Hi,

I've only been learning to code for a couple of weeks now. This is my first post. If I've posted incorrectly please do let me know.

So, I'vebeen typing this code:

counts = dict()
names = {"x", "y", "z", "x", "a"}
for name in names:
    counts[name] = counts.get(name, 0) + 1
print(counts)
and when running it, I'm getting this output:
Output:
D:\Python\helloworld\venv\Scripts\python.exe D:/Python/helloworld/app.py {'a': 1, 'z': 1, 'x': 1, 'y': 1} Process finished with exit code 0
Why isn't the code correctly counting the number of values in the "names" variable?

Thank you
Reply


Messages In This Thread
get method not counting number of strings in dictionary - by LearningTocode - Jun-13-2020, 10:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 335 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Trying to understand strings and lists of strings Konstantin23 2 756 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,510 May-01-2023, 09:06 PM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,753 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Counting number of words and organize for the bigger frequencies to the small ones. valeriorsneto 1 1,660 Feb-05-2021, 03:49 PM
Last Post: perfringo
  Counting Number of Element in a smart way quest 2 1,993 Nov-09-2020, 10:24 PM
Last Post: quest
  Counting the values ​​in the dictionary Inkanus 7 3,606 Oct-26-2020, 01:28 PM
Last Post: Inkanus
  counting items in a list of number combinations Dixon 2 2,070 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Pass Dictionary to Method is not possible Frank123456 1 2,349 Aug-19-2019, 10:18 AM
Last Post: buran
  Counting number of occurrences of a single digit in a list python_newbie09 12 5,464 Aug-12-2019, 01:31 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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