3

I have this output of code (used keyboard module):

[KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)]

How can I remove every 'KeyboardEvent' from this list?

2
  • 5
    Please be clear about your list elements. There appear to be no strings in this list; rather they're objects (individual instances of class KeyboardEvent). Commented Aug 24, 2017 at 23:38
  • The easiest way is just list = []. Is there a specific event that contains a certain keyboard action (such as "enter up") that you want removed? Commented Aug 24, 2017 at 23:44

5 Answers 5

6

How about using KeyboardEvent.name:

newList = [event.name for event in myList]

To get an even better result you can combine this with KeyboardEvent.event_type:

newList = [event.name + ' ' + event.event_type for event in myList]

Demo:

>>> myList
[KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down)]

>>> [event.name for event in myList]
['enter', 'h', 'h', 'e', 'e', 'y']

>>> [event.name + ' ' + event.event_type for event in myList]
['enter up', 'h down', 'h up', 'e down', 'e up', 'y down']
Sign up to request clarification or add additional context in comments.

Comments

3
a=[KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)]
a=[elem for elem in a if not isinstance(a, KeyboardEvent)]

This list comprehesion should work

4 Comments

Can't blame you for doing exactly what he asked, but this will produce an empty list, and that is clearly not the goal.
@ChristianDean I did not know that. Could you explain why?
@ChristianDean, is this what you wanted?
@Ajax1234 Sure. It's preferred because instance takes subclasses into account, while type does not. Here's the relevant part from the docs.
3

I would try regular expressions

import re

Foo = [KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)]

strList = []

for item in Foo:
  bar = re.sub('KeyboardEvent(\(.*?)', '', str(item))
  bar = re.sub('\)', '', bar)
  strList.append(bar)

print strList

1 Comment

Works fine but i still like @Vinícius Aguiar answer more.Upvote anyway.
1

Try to remove this using a loop:

list = [KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)]

for x in list:
    del list[str(x)]

5 Comments

What if it isn't a keyboard event?
It will still work. It works for deleting anything in a list/array.
then you may as well use list=[]
True, much simpler.
It's not recommended to use list as a variable name in Python
1

Or you could try this which actually removes the KeyBoard event as a string:

a=[KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)]
a=[str(elem).strip('KeyboardEvent') for elem in a]

1 Comment

['(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)'] , it only removes the first one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.