Python Arrays

JK JK 2018 
Created at
Updated at  
7,086 0 0

Arrays are used to store multiple values in one single variable:

cars = ["Ford", "Volvo", "BMW"]

 

Access the Elements of an Array

You refer to an array element by referring to the index number.

x = cars[0]
cars[0] = "Toyota"

 

The Length of an Array

Use the len() method to return the length of an array (the number of elements in an array).

x = len(cars)

 

Looping Array Elements

You can use the for in loop to loop through all the elements of an array.

for x in cars:
  print(x)

 

Adding Array Elements

You can use the append() method to add an element to an array.

cars.append("Honda")

 

Removing Array Elements

You can use the pop() method to remove an element from the array.

cars.pop(1)
cars.remove("Volvo")

Array Methods

Python has a set of built-in methods that you can use on lists/arrays.

MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the first item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list

 

Tags Python Python Arrays Facebook X
Comments 0
Similar posts
  1. Python Lambda
    7,101
  2. Python Classes/Objects
    7,466
  3. Python Functions
    7,062
  4. Python Inheritance
    7,185
  5. Python Iterators
    7,104
  6. Python While Loops/For Loops
    7,504
  7. Python Conditions and If statements
    7,128
  8. Python Polymorphism
    7,086
  9. Python Scope
    7,062
  10. Python Modules
    7,097
  11. Python Dictionaries
    7,053
  12. Python Sets
    7,090
  13. Python Tuples
    7,165
  14. Python Comparison Operators
    7,237
  15. Python Arithmetic Operators
    7,075
  16. Printing string n times
    7,065
  17. String concatenation by join()
    7,130
  18. Python Lists
    7,335
  19. Python String Operations
    7,406
  20. Python Data Types
    7,433
  21. Python Variables
    7,395
  22. Python Comments
    7,533
  23. Python Syntax
    7,405
  24. Python Getting Started
    7,453
  25. Python Introduction
    7,100
  26. What is Python?
    7,723
  27. Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering
    7,361
  28. Machine Learning Types and Programming Languages
    7,230
  29. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    7,888
  30. Mastering Excel Data Manipulation with Python
    7,067
  31. Challenge: One Code Problem Per Day
    1,093
  32. Clean Python Environments: The Power of venv vs. Docker
    826
  1. Clean Python Environments: The Power of venv vs. Docker
    826
  2. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    810
  3. Mastering Excel Data Manipulation with Python
    7,067
  4. Try...Catch Helps Ignoring Data Type Miss-Match Error in Python
    9,196
  5. RegExp example in Python to exclude javascript from HTML code
    7,053
  6. Python code to convert from Lunar to Solar
    7,723
  7. Python example to download webpage
    8,591
  8. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    7,888
  9. Python Modules
    7,097
  10. Python Scope
    7,062
  11. Python Polymorphism
    7,086
  12. Python Iterators
    7,104
  13. Python Inheritance
    7,185
  14. Python Classes/Objects
    7,466
  15. Python Lambda
    7,101
  16. Python Functions
    7,062
  17. Python While Loops/For Loops
    7,504
  18. Python Conditions and If statements
    7,128
  19. Python Dictionaries
    7,053
  20. Python Sets
    7,090
  21. Python Tuples
    7,165
  22. Python Comparison Operators
    7,237
  23. Python Arithmetic Operators
    7,075
  24. Printing string n times
    7,065
  25. String concatenation by join()
    7,130
  26. Python Lists
    7,335
  27. Python String Operations
    7,406
  28. Python Data Types
    7,433
  29. Python Variables
    7,395
Recently updated
  1. Watch This Dazzling Dancer’s Mind-Blowing Moves
    50
  2. Michael Jackson's Billie Jean
    131
  3. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    139
  4. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    418
  5. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    236
  6. The Evolution and Production Reality of Agentic AI
    214
  7. How to Activate or Waive Your UIUC Student Health Insurance
    297
  8. Complete Guide to Building a Machine Learning Model
    325
  9. My life cuts at Las Vegas during Thanksgiving day holiday
    7,407
  10. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    354
  11. Harness vs. OpenClaw: Two Very Different "Agents"
    980
  12. Clean Python Environments: The Power of venv vs. Docker
    826
  13. What is Docker? Why is Docker also useful in a development environment?
    621
  14. UIUC 2026-2027 Academic Calendar
    1,597
  15. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    810
  16. Open-Source LLMs: The AI Revolution
    762
  17. Resume 2.0: Leveling Up for My First Software Gig
    2,229
  18. Not everyone will understand what this man just did
    1,888
  19. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,596
  20. Unpacking IU's Shopper
    769
  21. Jackie Chan's Police Story: The Action Masterpiece
    664
  22. The IVE Story: Identity, 'I AM' Charts, and Influence
    968
  23. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,187
  24. Open Databases for Sex Crime Occurrences in the U.S.
    718
  25. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,653
  26. My First Day at University of Illinois-Urvana Champaign
    1,197
  27. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,134
  28. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,216
  29. Sipping the Stars: My Starbucks Adventure
    9,659
  30. Exciting explore at Sequoia National Park
    7,664
  31. My Life Shot at Death Valley
    1,713
  32. Ip Man fights with Muay Thai Master
    944
  33. Mad Clown - Don't Die
    1,027
  34. How to get Student Enrollment and Degree Verification at UIUC
    4,906
  35. LAX Thanksgiving Rush: A Joyful Reunion
    925
  36. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,131
  37. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    987
  38. 10CM - To Reach You (너에게 닿기를)
    1,165
  39. Feeling weak? Transform yourself at the UIUC ARC!
    1,573
  40. BOYNEXTDOOR - If I Say I Love You
    1,188
  41. The Future of Software Engineer - AI Engineering
    932
  42. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    920
  43. Lie - Legend song by BIGBANG
    7,820
  44. Why ROLLBACK is useful when you work with Google Gemini CLI?
    844
  45. Reimbursement after Vaccination at McKinley Health Center
    1,023
  46. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    968
  47. Common Questions from UIUC school life in terms of CS Program
    1,125
  48. UIUC Immunization Compliance
    1,213
  49. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,099
  50. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,096