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
#2
To print the total number of items in a list, use len(list_name). The count method is for counting how many times a specific item is in a list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
print(len(inv['cash']))
Is what I wanted. Thank you.

inv = {
    'cash': ['gold', 'gold'],
}

print(inv['cash'])
# This prints the 'cash' list

print(' '.join(inv['cash']))
# This prints the 'cash' list stripped of brackets and quotes

print(len(inv['cash']))
# This counts the number of all items in the 'cash' list and prints it

money = inv['cash']
# This assigns the 'cash' list to the money variable

print(money.count('gold'))
# This counts the number of 'gold' items in the cash list and prints it.
I made this. I think I understand it better now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 193 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Function to count words in a list up to and including Sam Oldman45 15 6,609 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 1,351 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Delete strings from a list to create a new only number list Dvdscot 8 1,547 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,249 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List all possibilities of a nested-list by flattened lists sparkt 1 922 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 884 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  Row Count and coloumn count Yegor123 4 1,332 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,618 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 4,882 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