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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
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
  removing one list element without using its index paul18fr 7 1,180 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  question about changing the string value of a list element jacksfrustration 4 2,103 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration
  extract an element of a list into a string alexs 5 3,692 Aug-30-2024, 09:24 PM
Last Post: alexs
  element in list detection problem jacksfrustration 5 1,856 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  list in dicitonary element problem jacksfrustration 3 1,638 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Function to count words in a list up to and including Sam Oldman45 15 10,534 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Find (each) element from a list in a file tester_V 3 2,204 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 3,426 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Row Count and coloumn count Yegor123 4 2,685 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  For Word, Count in List (Counts.Items()) new_coder_231013 6 6,786 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013

Forum Jump:

User Panel Messages

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