• 0

[Python] How to make empty array?


Question

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
Link to comment
https://www.neowin.net/forum/topic/627023-python-how-to-make-empty-array/
Share on other sites

5 answers to this question

Recommended Posts

  • 0
  Primexx said:
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

  • 0
  Primexx said:
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.

  • 0
  rpgfan said:
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.

  • 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.

  • 0
  rpgfan said:
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.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Amazon Deals: Samsung Q990F Q900F Q800F 2025 Dolby Atmos soundbars with wireless subwoofers by Sayan Sen While separate AV receivers with hi-fi speakers are generally the preferred way to listed to music and watch movies/shows by audiophiles, the more general folks often prefer soundbars instead as they offer a capable all in one solution that is still plenty good. Currently Nakamichi is running discounts on multiple products from its Dragaon lineup as well as its Shockwafe model. If you are looking for more options to choose from Samsung has its Q-series products at lowest prices (purchase links towards the end of article). Q990F The flagship Q990F is an 11.1.4 system and the single subwoofer unit on it houses two opposite-facing 8-inch subwoofer drivers. Thus, together they move around the same amount of air as a single 12-inch subwoofer unit. In addition to increasing the bass by +3 dB, dual opposing drivers are also said to help reduce vibrations of the subwoofer cabinet by cancelling out the resonance. Samsung also says that the bass is "AI-optimized" but we are not sure if it actually helps or if it's just a buzz term here. Aside from the bass, dialogue in movies is the second most important thing, and Samsung claims great vocal clarity from its front speakers thanks to AVA (Active Voice Amplifier) Pro feature that is said to detect noise disturbances and amplify dialogue to make it more audible over such surrounding noises. Q900F Feature wise the Q900F is similar to the 990F model except it has fewer channels and it is a 7.1.2 setup. Finally the Q800F is a 5.1.2 system and it has a passive radiator instead of the additonal subwoofer driver unit. Get the Samsung Q series soundbars at the links below: Samsung Q990F 11.1.4ch Wireless Dolby Atmos, Q-Symphony, Game Mode Pro, Adaptive Sound (HW-Q990F, 2025): $1497.99 (Shipped and Sold by Amazon US) Samsung HW-Q900F 7.1.2 ch Wireless Dolby Atmos, Q-Symphony: $997.99 (Shipped and Sold by Amazon US) Samsung Q800F 5.1.2ch Q Series Soundbar + Subwoofer, Wireless Dolby Atmos, Q-Symphony, Game Mode Pro, Smart Integration (HW-Q800F, 2025): $697.99 (Shipped and Sold by Amazon US) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • They are shifting into AI now. Don't you see?
    • Exactly. No need to pay to rent a license. I'd rather own it.
  • Recent Achievements

    • One Month Later
      Helen Shafer earned a badge
      One Month Later
    • One Month Later
      ambani880 earned a badge
      One Month Later
    • Week One Done
      ambani880 earned a badge
      Week One Done
    • First Post
      artistro08 earned a badge
      First Post
    • First Post
      paul29 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      498
    2. 2
      ATLien_0
      223
    3. 3
      Michael Scrip
      196
    4. 4
      Xenon
      160
    5. 5
      +FloatingFatMan
      138
  • Tell a friend

    Love Neowin? Tell a friend!