• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to create a Python Dictionary

by admin

Dictionaries in python are quite similar to arrays in how they look however arrays are indexed by a range of numbers i.e. 1-10, but dictionaries are indexed by their key. You can set these keys to immutable data types only. They can also be known as key pair arrays.

You will be used to seeing this to create a standard array using square brackets.

 test_array = []

If you want to create a dictionary you use curly brackets.

 test_dictionary = {}

The format of a dictionary looks like this.

{'TEST': 1}

With TEST being the key and 1 the pair.

If you want to store values in a dictionary

test_dictionary["KEY"] = "Pair"
Note: You don’t have to just use string other data types in place of each one.

To iterate through a dictionary:

for key, value in test_dictionary.items():
     print('{0} links to {1}'.format(key, value)
Note: Using format when printing strings is considered good practice.

Filed Under: Linux

Some more articles you might also be interested in …

  1. a2disconf Command Examples in Linux
  2. CentOS / RHEL 7 : How to boot into rescue mode from installation DVD/ISO
  3. homectl Command Examples in Linux
  4. Command ‘df -i’ Shows ‘Inode=0’ on BTRFS File System
  5. logrotate: command not found
  6. Magento 2 Rabbitmq configuration
  7. chromium-browser: command not found
  8. How to use “yum downloadonly” to download a package without installing it
  9. blkid: command not found
  10. How to prevent SSH connections from disconnecting due to inactivity when using MobaXterm

You May Also Like

Primary Sidebar

Recent Posts

  • “aws s3 mv” Command Examples
  • “aws s3 mb” Command Examples
  • “aws s3 ls” Command Examples
  • “aws s3 cp” Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright