Python Forum
List items verification for Integer type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List items verification for Integer type
#5
One way (which does not require to define possible classes in chain of if-s) is to use defaultdict and class name:

>>> import collections
>>> lst = ['abc', 1, 2, 3, 4.3, [42]] 
>>> types = collections.defaultdict(list)
>>> for item in lst: 
...     types[type(item).__name__].append(item)
...
>>> types
defaultdict(list, {'str': ['abc'], 'int': [1, 2, 3], 'float': [4.3], 'list': [[42]]})
>>> for k, v in types.items(): 
...     print(f'{k} --> {v}') 
...                                                                 
str --> ['abc']
int --> [1, 2, 3]
float --> [4.3]
list --> [[42]]
>>> types['int']                                                    
[1, 2, 3]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: List items verification for Integer type - by perfringo - Jan-17-2020, 01:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 193 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,352 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Finding combinations of list of items (30 or so) LynnS 1 885 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  search a list or tuple for a specific type ot class Skaperen 8 1,945 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,618 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get list of exactly 10 items? Mark17 1 2,537 May-26-2022, 01:37 PM
Last Post: Mark17
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,175 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,884 May-07-2022, 08:07 AM
Last Post: menator01
  how to assign items from a list to a dictionary CompleteNewb 3 1,587 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Reading list items without brackets and quotes jesse68 6 4,647 Jan-14-2022, 07:07 PM
Last Post: jesse68

Forum Jump:

User Panel Messages

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