Morelia spilota spilota

Python length

How do you get the length of a list or tuple or array in Python?

Let me clarify something at the beginning, by array, you probably mean list in Python. list is the equivalent of arrays in JavaScript or PHP. Arrays in Python is an altogether different thing.

Ok, having cleared that, getting the the size of a list or tuple (or array, if you will), is pretty straighforward. You just call the len function on the object, and there you have it's size. Examples of list and tuple size / lengths are given below.

Size of list

> fighters = ['bruce', 'chuck', 'benny']
> len(fighters)
3

Size of tuple

> movie = ('Terminator', 'James Cameron', 'Arnold Schwarzenegger')
> len(movie)
3

So there it is, you just call the global function len on the list or tuple and you get its size / length.

Did you expect to have something like list.len or tuple.len instead? Actually there is something like that, but it is called list.__len__ or tuple.__len__. And what len really does is, it takes the object and tries to call the objects's __len__ method. So essentially, len works only on objects that has a __len__ method. If you have a custom object that has the __len__ method, you can call len even on that.

So you learnt: to get the size or length of a list or tuple in Python (you probably didn't mean Python array), you just call the len function on the object.

Notes

  1. If you came from PHP / JavaScript, by array, probably you mean list in Python.
  2. You can make your custom objects return values to the len function.

Exercise

  1. Create a custom object which returns weird values for len and shock your family and friends.
  2. Why isn't there list.len or tuple.len, instead there is list.__len__ or tuple.__len__?


You might also like
1.5 year old ball python weight+length
1.5 year old ball python weight+length
Python 38 Getting Length
Python 38 Getting Length
Python tutorial: Calculate Spline length (3 ways)
Python tutorial: Calculate Spline length (3 ways)
Master Lock Master Lock 8413DPF Python Adjustable Locking Cable, 6-Foot x 3/8-inch
Home Improvement (Master Lock)
  • 6-foot adjustable locking cable
  • Multipurpose lock for bikes, grills, trailers, ladders, chain-link applications
  • Patented locking mechanism adjusts from 1 to 6 feet and holds steel cable tight
  • Re-keyable cylinders for keyed alike convenience
  • 3/8-inch cable; lifetime warranty


Master Lock Master Lock 8419DPF Python Adjustable Locking Cable, Braided Steel, Yellow and Black Contractor Color, 6-Feet x 5/16-inch
Home Improvement (Master Lock)
  • Patented locking mechanism holds the cable tight at any length from 6-inch (15-Centimeter) to 6-Feet (1.8-Meter)
  • Cable end threads through places other cables cannot
  • 5/16-inch (8mm) braided steel cable
  • Vinyl coated cable and ABS plastic lock body bumper in yellow and black contractor color scheme
  • Reliable pin tumbler mechanism

Python Basics - Find the length of a string
Python Basics - Find the length of a string
Finding the length of a Python String
Finding the length of a Python String
985 LB PYTHON The Length of a Four Story Building
985 LB PYTHON The Length of a Four Story Building
Python Products Inc. Python No Spill Clean and Fill Aquarium Gravel Tube, 24-Inch
Pet Products (Python Products Inc.)
  • Packaged with 2-1/2 feet of tubing and a female connector
  • Made of durable plastic
  • Compatible with the No Spill Clean and Fill system
Master Lock Master Lock - Python Trail Camera Adjustable Camouflage Cable Locks 8418KA-2 CAMO
Automotive Parts and Accessories (Master Lock)
  • You can order up to four (4) of these cable locks keyed alike, or said a different way, two 2-packs.
  • In MANY INSTANCES we can get you eight (8) keyed alike or four 2-packs.
  • Cut resistant 5/16 (8mm) braided steel cable. Patented locking mechanism holds cable secure at any position from 1 (30cm) to 6 (1.8m) for infinite locking positions.
  • Tiedown convenience - cinches tight for improved security and no flapping within pickup bed or on trailer.
  • Weather Tough® aluminum alloy lock, cylinder shutter and vinyl coated cable
Master Lock Master Lock - Python Adjustable Cable Locks 8413-30
Automotive Parts and Accessories (Master Lock)
  • Patented locking mechanism holds cable secure at any position from 1 (30cm) to 30 (3.7m) for infinite locking positions.
  • Cut resistant 3/8 (10mm) braided steel cable 30 ft long.
  • Ideal for locking down multiple items; such as plant equipment, job sites, motorcycles, lawn furniture, and much more!
  • Weather Tough® aluminum alloy lock, cylinder shutter and vinyl coated cable.
  • Tiedown convenience - cinches tight for improved security and no flapping within pickup bed or on trailer.


GIANNI VERSACE KNEE LENGTH PYTHON SKIN BOOTS
GIANNI VERSACE KNEE LENGTH PYTHON SKIN BOOTS
PYTHON Tutorial : Python string length
PYTHON Tutorial : Python string length
Basic Python Tutorial 10 - In operator and length function
Basic Python Tutorial 10 - In operator and length function
Related Posts