Python arrays/lists

Let me keep this brief, I like the way javascript handles arrays. Specifically, as follows:

var myDB = new Array();
myDB[0] = "";
myDB[1] = "another entry";

The reason I like this, and why I am having troubles is that I am trying to replicate this in python. The reason I want to replicate this in python is because I am working on a psuedo operating system, and would like to allow users to add new entries to the variable that don’t yet exist. If I could make it work, this is what it would look like:

myDB = #Array

userAdd = input('what would you like to add to the array?')
myDB[myDB.length + 1] = userAdd

I hope this makes sense.

To be a little more clear – I am wanting this to act as a sort of session memory. The only way I can realistically think to make things work currently is to limit users to the amount of memory spaces I create as as many variables allocated to it, such as follows:

myDB0 = ''
myDB1 = ''
myDB2 = ''
myDB3 = ''
myDB4 = ''

editDB = input('what slot would you like to change?')
if editDB == '1':
     addMem = input('what would you like it to be?')
     myDB0 = addMem
#And et cetera for each memory space

anyone got any ideas?

I’m confused, that’s not valid Python.

aah, var!

This in Python would just be

myDB = {} # dictionary, not list/array
myDB[0] = ""
myDB[1] = "another entry"
myDB = {}

userAdd = input('what would you like to add to the array?')
myDB[len(myDB) + 1] = userAdd

Also @DaveBoyo, the example above is 1-indexed, which, why would you ever do that lol. I would recommend not adding the + 1 tbh

2 Likes

Perfect!

Yes, the python code wasn’t valid, but I guess that was sort of my point lol – I wasn’t sure what the appropriate code would be for that type of formatting.

Much appreciated!

p.s. “AH VAR!” Indeed lol

1 Like

Here is a python version using lists instead:

my_db = [None] * 10
my_db[0] = ""
my_db[1] = "another entry"

Of course, this is not good python. Just use my_db.append(...) instead of my_db[i] = ...

my_db = []
user_add = input('what would you like to add to the array?')
my_db.append(user_add)

To edit,

try:
    index = int(input('what slot would you like to change'))
except ValueError:
    print('invalid input')
else:
    if 0 <= index < len(my_db):
        item = input('what would you like it to be?')
        my_db[index] = item
    else:
        print('slot does not exist')
3 Likes

Wow, that syntax is so weird…

my_db = [None for _ in range(10)]
1 Like

Sequence multiplication is occasionally useful. Here, I would say it is more readable than a comprehension.

1 Like

Yeah, but it looks like it would create 10 different lists tbh

1 Like

Wait but what is the purpose of [None] in this case?

And that is just… why use _? And the [None]!?

We use None to create a list with 10 items, and the _ is for variables which you will never use.

3 Likes
[None] * 5 == [None, None, None, None, None] == [None for _ in range(5)]
[] * 5 == []
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.