u/Advanced_Glass5563

Hi everyone,

While studying python I have the feeling it it not the most efficient programming language in terms of computing resource requirements.

In order to make sure I learn to code efficiently I'm using a Dell Inspiron from 2021 whose processes typically occupy 80-90% of RAM

I have created a very simple lexicon.py file with a class that builds a list of tuples based on a dictionary with multiple values per each key.

import time

class Lexicon:
def __init__(self):
self.list_tuples = []
self.game_lexicon = {
'direction': ['north', 'south', 'east', 'west' , 'down', 'up','left','right','back'],
'verb': ['go', 'stop','kill', 'eat'],
'stop': ['the', 'in', 'of', 'from', 'at', 'it'],
'nouns': ['door', 'bear', 'princess', 'cabinet'],
'number': [0,1,2,3,4,5,6,7,8,9]
}

for key in self.game_lexicon.keys():
values = self.game_lexicon.get(key)
for i in values:
self.list_tuples.append((key, i))

start_time = time.time()
print("--- %s seconds ---" % (time.time() - start_time))

print("Hi there! How are you today?")
print("Please let me know how can I help:")

lexicon = Lexicon()
print("--- %s seconds ---" % (time.time() - start_time))

When I execute the file I receive below stats:

python lexicon.py

--- 1.1920928955078125e-06 seconds ---

Hi there! How are you today?

Please let me know how can I help:

--- 0.008804082870483398 seconds ---

As you can see, it takes more than 1 second for the first print. Is this due to inefficiency on the code itself? More in general, is there a better, more efficient way to build the list of tuples than the one I have used?

reddit.com
u/Advanced_Glass5563 — 9 days ago

Hello , I'm fairly new to python and playing with some exercises provided Zed Shaw on his book "Learn python the hard way".

I was trying to repurpose for fun his game for my little nephews and I ended up creating a list which string elements are longer than 80 chars.

Eg

class Death(object):

quips = [

"Oh ... seems like Snow , your puppy, is better than you at this game. Retry with something better" ,

	*"Hopefully you are better at school than at this game",*

	*"Hmmm .... you better think of something smarter"*

]

def enter(self):

print( Death.quips[randint(0, len(self.quips) -1)])

exit(1)

As you might notice the first element of the list is longer than 80 chars. How can I manage this string in 2 rows both on the list declaration and on the print screen (command line) when the python code is executed ?

reddit.com
u/Advanced_Glass5563 — 16 days ago