• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

[Python] How to make empty array?

Question

primexx    372

The geniuses at my uni decided to teach python instead of PHP, so now I'm stuck with it for my assignments. Anywho, how do you make an empty array in python? Everything I've searched up only tells you how to make them manually, hard coding all the items, not a single one tells you how to just make an empty array of x items. Anyone here know? Thx!

edit: nm got it

ary = []; #make list of 0 length
ary.append('whatever'); #add items

Edited by Primexx

Share this post


Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0
72001    4
The geniuses at my uni decided to teach python instead of PHP, so now I'm stuck with it for my assignments. Anywho, how do you make an empty array in python? Everything I've searched up only tells you how to make them manually, hard coding all the items, not a single one tells you how to just make an empty array of x items. Anyone here know? Thx!

edit: nm got it

ary = []; #make list of 0 length
ary.append('whatever'); #add items

I am not a big python guy but I think I found it for you...

http://www.rexx.com/~dkuhlman/python_101/python_101.html

>>>

2.2.2 Sequences

2.2.2.1 What

There are several types of sequences in Python. We've already discussed strings. In this section we will describe lists and tuples. See http://www.python.org/doc/current/lib/typesseq.html for a description of the other sequence types (e.g. buffers and xrange objects).

Lists are dynamic arrays. They are arrays in the sense that you can index items in a list (for example "mylist[3]") and you can select sub-ranges (for example "mylist[2:4]"). They are dynamic in the sense that you can add and remove items after the list is created.

Tuples are light-weight lists, but differ from lists in that they are immutable. That is, once a tuple has been created, you cannot modify it. You can, of course, modify any (modifiable) objects that the tuple refers to.

Capabilities of lists:

Append items.

Insert items.

Add a list of items.

Capabilities of lists and tuples:

Index items.

Select a subsequence of items (also known as a slice).

Iterate over the items in the list or tuple.

2.2.2.2 When

Whenever you want to process a colletion of items.

Whenever you want to iterate over a collection of items.

Whenever you want to index into a collection of items.

2.2.2.3 How

To create a list use:

>>> items = [111, 222, 333]

>>> items

[111, 222, 333]

To add an item to the end of a list, use:

>>> items.append(444)

>>> items

[111, 222, 333, 444]

To insert an item into a list, use:

>>> items.insert(0, -1)

>>> items

[-1, 111, 222, 333, 444]

You can also push items onto the right end of a list and pop items off the right end of a list with append and pop.

>>> items.append(555)

>>> items

[-1, 111, 222, 333, 444, 555]

>>> items.pop()

555

>>> items

[-1, 111, 222, 333, 444]

And, you can iterate over the items in a list with the for statement:

>>> for item in items:

... print 'item:', item

...

item: -1

item: 111

item: 222

item: 333

item: 444

>>>

Sorry if that was no help :p

Share this post


Link to post
Share on other sites
  • 0
rpgfan    0
The geniuses at my uni decided to teach python instead of PHP, so now I'm stuck with it for my assignments. Anywho, how do you make an empty array in python? Everything I've searched up only tells you how to make them manually, hard coding all the items, not a single one tells you how to just make an empty array of x items. Anyone here know? Thx!

edit: nm got it

ary = []; #make list of 0 length
 ary.append('whatever'); #add items

You definitely got it, though last I checked Python didn't use semicolons at the end (PHP habit?) :p

The same technique for arrays (known as lists in Python) can be used to create empty dictionaries (a.k.a. associative arrays, hash maps, etc.) or tuples:

lst = [] #empty list
dic = {} #empty dictionary
tup = () #empty tuple

Also, you can find the length using the len() function and the type using the type() function:

print type([]) #<type 'list'>
print type({}) #<type 'dict'>
print type(()) #<type 'tuple'>

#you can do the same with the len() function if you want
#  but I'm not going to

The idea is that you're calling the initializers/constructors for each object:

[] = list()

{} = dict()

() = tuple()

I'm no Python expert, but I know about those things. :p

It is really quite a nice language once you get used to it, but I can't say it is better or worse than PHP because I use both. There isn't anything better than PHP for websites, though I don't have anything against ASP/ASP.NET or CGI/Perl. Python is a great multi-purpose scripting language though - a nice replacement for the normal command line in Windows.

Share this post


Link to post
Share on other sites
  • 0
primexx    372
You definitely got it, though last I checked Python didn't use semicolons at the end (PHP habit?) :p

The same technique for arrays (known as lists in Python) can be used to create empty dictionaries (a.k.a. associative arrays, hash maps, etc.) or tuples:

lst = [] #empty list
dic = {} #empty dictionary
tup = () #empty tuple

Also, you can find the length using the len() function and the type using the type() function:

print type([]) #<type 'list'>
print type({}) #<type 'dict'>
print type(()) #<type 'tuple'>

#you can do the same with the len() function if you want
#  but I'm not going to

The idea is that you're calling the initializers/constructors for each object:

[] = list()

{} = dict()

() = tuple()

I'm no Python expert, but I know about those things. :p

It is really quite a nice language once you get used to it, but I can't say it is better or worse than PHP because I use both. There isn't anything better than PHP for websites, though I don't have anything against ASP/ASP.NET or CGI/Perl. Python is a great multi-purpose scripting language though - a nice replacement for the normal command line in Windows.

honestly, I hate python because of its lack of brackets (I also hate VB, etc for the same reason), lack of some functions (no switch or do-while?!), and majorly ****ed up syntax. And I had to guess-and-check to figure the list thing out, absolutely nowhere I searched ever said "Use var = []; to make an empty list, to which you could add stuff later".

oh, yea, the semicolons are from every single other language I use, so I tack them on (the interpreter doesn't complain about that, so it's all good) to read the code more comfortably...it just doesn't feel right without.

Share this post


Link to post
Share on other sites
  • 0
rpgfan    0

You can use switch statements (sort of) via the combination of dictionaries and lambda functions. The idea that I found can be seen at http://simonwillison.net/2004/May/7/switch/ if you really want it, but I think if-elif-else statements work just as well. As one commenter mentioned, using it in combination with try-except blocks (and sometimes even a try-except-else-finally blocks) can help to create the "default:" part if necessary. Check out http://docs.python.org/ref/try.html for some more information on the way try-except-else-finally is used.

Share this post


Link to post
Share on other sites
  • 0
primexx    372
You can use switch statements (sort of) via the combination of dictionaries and lambda functions. The idea that I found can be seen at http://simonwillison.net/2004/May/7/switch/ if you really want it, but I think if-elif-else statements work just as well. As one commenter mentioned, using it in combination with try-except blocks (and sometimes even a try-except-else-finally blocks) can help to create the "default:" part if necessary. Check out http://docs.python.org/ref/try.html for some more information on the way try-except-else-finally is used.

yea I did use the dictionary + lambda for switch, I never thought of using a try-except for it though (never heard of finally, until now). seems like the dictionary is easier in any case.

if-else might work for a few options, but it gets really clumsy after you have tens of choices, and lambda has it's own drawbacks, so it's still lacking on python's part to not have something as basic as a switch. I just like the traditional syntax a lot more.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.