Python Forum
count each element in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
count each element in a list
#1
hi guys , i want to print same element in each item in a list,
my code returns nothing except if i print list2,
it should return 3 elements in first item and 2 elements in a second one
    list1=[10,20,30,40]
    list2 =[[10,20,30,1],[10,20,1,2]]
    #print(list2)
    for i in list2:
        num= i.count(list1)
    if (num > 2):
        print(num)
Reply
#2
I suspect that you will get a result if you do:
print(len(list1))
print(len(list2))
Although i see 4 elements in list1.
Count is not necessary.
Paul
Reply
#3
If you want to print the same element in both lists, you can do:
for i in list1 :
    if i in list2 :
        #your code
Reply
#4
Thanks for your response,len() just return number of item in list2 which is 2, i want if there is/are same element on each item from list2 will be printed, using list1 as comparison
Reply
#5
>>> list1=[10,20,30,40]
>>> list2 =[[10,20,30,1],[10,20,1,2]]
>>> for lst in list2:
...   len([value for value in lst if value in list1])
...
3
2
Reply
#6
(May-01-2020, 10:34 AM)pyzyx3qwerty Wrote: If you want to print the same element in both lists, you can do:
for i in list1 :
    if i in list2 :
        #your code
it returns nothing since list2 has no same item from list1 it has only 3 same elements that which i want to be printed
Reply
#7
(May-01-2020, 10:45 AM)glennford49 Wrote:
(May-01-2020, 10:34 AM)pyzyx3qwerty Wrote: If you want to print the same element in both lists, you can do:
for i in list1 :
    if i in list2 :
        #your code
it returns nothing since list2 has no same item from list1 it has only 3 same elements that which i want to be printed

I know, as i misread your thread as print the elements, not print the number of elements
Reply
#8
(May-01-2020, 10:47 AM)pyzyx3qwerty Wrote:
(May-01-2020, 10:45 AM)glennford49 Wrote: it returns nothing since list2 has no same item from list1 it has only 3 same elements that which i want to be printed

I know, as i misread your thread as print the elements, not print the number of elements

its just fine, it just happen that i can hardly fix my code, thanks for your response

(May-01-2020, 10:39 AM)anbu23 Wrote:
>>> list1=[10,20,30,40]
>>> list2 =[[10,20,30,1],[10,20,1,2]]
>>> for lst in list2:
...   len([value for value in lst if value in list1])
...
3
2

how to return the exact same element found in list2?
Reply
#9
(May-01-2020, 10:50 AM)glennford49 Wrote: how to return the exact same element found in list2?

Remove len()
Reply
#10
(May-01-2020, 11:09 AM)anbu23 Wrote:
(May-01-2020, 10:50 AM)glennford49 Wrote: how to return the exact same element found in list2?

Remove len()

thumbs up, this saves my day!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sume Every 10 element in the list and Divided of Sum quest_ 7 148 Nov-27-2020, 10:58 AM
Last Post: perfringo
  Counting Element in Multidimensional List quest_ 1 96 Nov-25-2020, 10:00 PM
Last Post: quest_
  Adding List Element if Second part of the List Elements are the Same quest_ 3 130 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Removing element from list squall 6 188 Nov-22-2020, 09:34 PM
Last Post: jefsummers
  2d Array adds last element to entire list waiteup 2 105 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  How to get index of minimum element between 3 & 8 in list Mekala 2 171 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  Count number of occurrences of list items in list of tuples t4keheart 1 118 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  Problem printing last element from a list tester_V 3 200 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Finding an element in a 1d list in a 2d array lionrocker221 0 376 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  How to get unique entries in a list and the count of occurrence james2009 3 411 Jun-22-2020, 01:31 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