Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / How to declare and add items to an array in Python?

How to declare and add items to an array in Python?

August 20, 2021 by James Palmer

{} represents an empty dictionary, not an array/list. For lists or arrays, you need [].
To initialize an empty list do this:
my_list = []

or
my_list = list()

To add elements to the list, use append
my_list.append(12)

To extend the list to include the elements from another list use extend
my_list.extend([1,2,3,4])
my_list
–> [12,1,2,3,4]

To remove an element from a list use remove
my_list.remove(2)

Dictionaries represent a collection of key/value pairs also known as an associative array or a map.
To initialize an empty dictionary use {} or dict()
Dictionaries have keys and values
my_dict = {‘key’:’value’, ‘another_key’ : 0}

To extend a dictionary with the contents of another dictionary you may use the update method
my_dict.update({‘third_key’ : 1})

To remove a value from a dictionary
del my_dict[‘key’]

If you do it this way:
array = {}

you are making a dictionary, not an array.
If you need an array (which is called a list in python ) you declare it like this:
array = []

Then you can add items like this:
array.append(‘a’)

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in