Home > Software engineering >  Store the result of a function in a list python
Store the result of a function in a list python

Time:10-17

Intro I got a function that parse the output from the daemon socket. I use it to catch the keys pressed on a IR remote.

def getKey():
    while True:
        data = sock.recv(128)
        data = data.strip()

        if (len(data) > 0):
            break

    words = data.split()
    return words[2], words[1]

key = getKey()
print(key)

Problem Function always returns a single string object

Output:

1
<class string>
2
<class string>
7
<class string>

Question How can I store all those string objects to a single list object for further use?

Like so:

[1,2,7]
<class list>

CodePudding user response:

def getKey():
    while True:
        data = sock.recv(128)
        data = data.strip()

        if (len(data) > 0):
            break

    words = data.split()
    return words[2], words[1]

keys = []
keys.append(getKey())
keys.append(getKey())
keys.append(getKey())
print(keys)
  • Related