0

How to find and group items in lists?

I really need help with that, i have a list with some items, and there are a number of items that are the same, but they are not in order. I want to print the number of items of each type along with its name, is it logic to use for loop in that? or should i use other loop?

6th Jul 2016, 7:15 PM
Carlos Hidalgo
Carlos Hidalgo - avatar
10 Answers
+ 2
I hit the Interpreter, and here is what I've got for you. I used a dictionary instead of a list so that I could add to the value as you requested. http://www.sololearn.com/app/JUMP_LINK__&&__python__&&__JUMP_LINK/playground/cG2WmjauI5dJ/
7th Jul 2016, 6:34 AM
Yarden Akin
Yarden Akin - avatar
+ 1
So you need it done with lists? Well, you can have two separate lists, one with the keys, and one with the values. They will correspond with indexes. Here is what I have got for you. http://www.sololearn.com/app/JUMP_LINK__&&__python__&&__JUMP_LINK/playground/cv9vLezX2RHh/
7th Jul 2016, 5:54 PM
Yarden Akin
Yarden Akin - avatar
0
Perhaps have a separate integer managing number or the same item? I might need more details on the scenario to understand it properly. EDIT: Resolved using a dictionary
7th Jul 2016, 5:19 AM
Yarden Akin
Yarden Akin - avatar
0
Its like: list=[itemA,itemB,itemB,itemC,itemA] I want to print the content of the list like: 2 ItemA 2 ItemB 1 ItemC (maybe is a silly question but i cant think of an answer)
7th Jul 2016, 5:23 AM
Carlos Hidalgo
Carlos Hidalgo - avatar
0
Just checking in, did that work all right? Any questions I can clear up?
7th Jul 2016, 6:56 AM
Yarden Akin
Yarden Akin - avatar
0
It was very helpful, the problem is solved. But, are key and val only call-able in dictionaries? or they can be used in lists? Edit: Maybe i can use the index in the list to iterate and then add the items that are the same
7th Jul 2016, 2:08 PM
Carlos Hidalgo
Carlos Hidalgo - avatar
0
Dictionaries are the only group type that can access the key/Val system. The catch is that they have no order, and as such cannot be accessed using indexes, just keys. The only way to can think of to do what I have done here with lists is to have two separate lists with corresponding indexes. This is better explained at http://www.python-course.eu/dictionaries.php However, I don't see why a dictionary wouldn't work for you.
7th Jul 2016, 3:00 PM
Yarden Akin
Yarden Akin - avatar
0
I get the idea, i know that is better to use a dictionary, maybe im trying to make an impossible program, i will investigate more, so i can do it myself. The problem is that im trying to solve a challenge in a webpage called usingpython.com, and it specifies that i have to use lists. But thanks for the help anyways.
7th Jul 2016, 3:43 PM
Carlos Hidalgo
Carlos Hidalgo - avatar
0
Wow, that's what i was asking for, it works! Maybe I'll have to change a little my inicial code to make it work with both lists, but i never thought it would be so simple, thanks a lot! 😃
7th Jul 2016, 6:02 PM
Carlos Hidalgo
Carlos Hidalgo - avatar
0
No problem! I enjoyed the practice. Perhaps I will check out usingpython.com, get the brain working
7th Jul 2016, 6:05 PM
Yarden Akin
Yarden Akin - avatar