Home > Blockchain >  How I can count the number of times that a element repeat inside a list, if the element itself is a
How I can count the number of times that a element repeat inside a list, if the element itself is a

Time:11-23

list = [[ '1', '2'], [ '1', '2'], [ '1', '2'], ['3','4'], ['3','4'],['3','4'],['3','4']]

I would like to print out print( 'the element [ '1', '2'] repeat 3 times')

CodePudding user response:

Same as with any other element check:

>>> x.count(['1', '2'])
3

You can wrap this up in a function:

def f(v):
  print(f'Item "{v}" appears {x.count(v)} times in {x}')
  •  Tags:  
  • list
  • Related