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
#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


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

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 221 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Function to count words in a list up to and including Sam Oldman45 15 6,743 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,384 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Delete strings from a list to create a new only number list Dvdscot 8 1,591 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,288 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List all possibilities of a nested-list by flattened lists sparkt 1 945 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 894 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  Row Count and coloumn count Yegor123 4 1,372 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,654 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,932 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