Python Forum
Count the number of items in a nested list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count the number of items in a nested list
#1
inv = {
    'cash': ['gold', 'gold'],
}

print(inv['cash'])

print(' '.join(inv['cash']))

print(' '.count(inv['cash']))
I can print the 'cash' list.

I can print the 'cash' list with the brackets and quotations stripped off.

If I try to count the number of items in the 'cash' list, Python gets angry.

How do I count the number of items in the 'cash' list? I just want it to return a number. Can this be done in one line using indexing syntax?

I've been reading tutorials online that show how to count items in nested lists and they are huge monstrosities. There has to be a simple way to do it. I'm surprised my third print statement doesn't work. It says it wants it to be a string, not a list. Trying to convert it to a string:

print(' '.count(str(inv['cash'])))

returns 0 when it should return 2.

Any help would be appreciated.
Reply


Messages In This Thread
Count the number of items in a nested list - by iofhua - Jun-07-2019, 06:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What is a faster way to deep copy a nested list without using .deepcopy()? Shervin_Ataeian 1 1,823 Oct-13-2024, 01:28 PM
Last Post: Pedroski55
  Function to count words in a list up to and including Sam Oldman45 15 11,300 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 3,185 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Delete strings from a list to create a new only number list Dvdscot 8 3,651 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 7,961 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List all possibilities of a nested-list by flattened lists sparkt 1 1,893 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 1,622 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  Row Count and coloumn count Yegor123 4 3,059 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  For Word, Count in List (Counts.Items()) new_coder_231013 6 7,533 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 7,024 Jul-01-2022, 01:23 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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