+ 2

How to make this code?

l=[1,2,3,4] for i in l: x=l.count(i) print(x) ''' i want x to be a list i mean instead of printing each x on a different line i want it to be printed in a list [1,1,1,1]''' #then after i get the list for i in x: if all(i<2): print("none") else: print("anything") #how do i make this fit in the code

2nd Jan 2020, 10:04 AM
Pattern
Pattern - avatar
15 Réponses
+ 8
Allready a lot of answers. May be you look for something like this: #l=[1,2,3,4,2,3,1,0,1] l=[1,2,3,4,5,6,7,0] res = [] for i in set(l): # it needs a set to iterate because of duplicates: res.append(l.count(i)) print(res) if all(j < 2 for j in res): print('output-1') else: print('output-2')
2nd Jan 2020, 11:41 AM
Lothar
Lothar - avatar
+ 5
I made it using a dictionary, the condition checks and shows "None" if all values in dictionary is less than 2, "Anything" otherwise. l = [1,2,3,4,1,3] d = { v : l.count(v) for v in set(l) } #print(d) if( all(v < 2 for v in d.values()) ): print("None") else: print("Anything")
2nd Jan 2020, 11:28 AM
Ipang
+ 3
No need, loop does not make any difference All I am saying is appending x in any other list like this👇 https://code.sololearn.com/cwTIibcJB9uh/?ref=app
2nd Jan 2020, 10:21 AM
Arsenic
Arsenic - avatar
+ 2
As 'i' is never less that 2 so it was returning false so this statement (all(i<2)) was treated as all(false) That's why it was giving error as bool is not itreatable
2nd Jan 2020, 10:51 AM
Arsenic
Arsenic - avatar
+ 2
all() function checks wether all the members of LIST are iterable or not , but 'i' is not list
2nd Jan 2020, 10:53 AM
Arsenic
Arsenic - avatar
+ 1
Just make 'x' an empty list and then append it everytime you want add an element in it
2nd Jan 2020, 10:07 AM
Arsenic
Arsenic - avatar
+ 1
Oh I forgot to print l2[] instead of x Well thanks for pointing that out Now I have updated the code you can have a look at it now
2nd Jan 2020, 10:32 AM
Arsenic
Arsenic - avatar
+ 1
Ok what about the bottom part dude
2nd Jan 2020, 10:35 AM
Pattern
Pattern - avatar
+ 1
Saja Ali now the code is fully finished. The error was occuring because you were checking is 'i' is iterable or not but as 'i' is not a list so it was giving an erro
2nd Jan 2020, 10:42 AM
Arsenic
Arsenic - avatar
+ 1
😁
4th Jan 2020, 10:01 AM
Si Mö
Si Mö - avatar
0
Do I have to use while loop
2nd Jan 2020, 10:18 AM
Pattern
Pattern - avatar
0
But it still prints 1 1 1 1 Not a list and an error says bool object is not iterable
2nd Jan 2020, 10:30 AM
Pattern
Pattern - avatar
0
Doesn't if all (i <2) mean if all the elements in the new list smaller than 2 doesn't mean that
2nd Jan 2020, 10:48 AM
Pattern
Pattern - avatar
- 1
phl 50 65 78652 in all
3rd Jan 2020, 1:57 AM
Aaliyah