• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / List vs Dictionary in Python

List vs Dictionary in Python

Author: Aditya Raj
Last Updated: May 12, 2023

Python lists and dictionaries are two of Python’s most used data structures. In this article, we will discuss list vs dictionary in Python to compare definition, syntax, mutability, and performance.

Table of Contents
  1. Python List vs Dictionary Summary
  2. List vs Dictionary: Syntax and Definition
  3. Python List vs Dictionary: Mutability
  4. List vs Dictionary: Performance
  5. When to Use List vs Dictionary in Python?
  6. Conclusion

Python List vs Dictionary Summary

Following are some of the major differences between a Python list and a dictionary.

Python ListPython Dictionary
Lists are defined using square brackets [] and the list() function.A Python dictionary is defined using the curly braces {} or the dict() function.
A Python list allows random access to elements using indices.A dictionary allows random access to values using keys.
Lists allow duplicate elements.A dictionary doesn’t allow duplicate keys. It allows duplicate values.
Lists provide faster performance while initialization, accessing elements, and iterating.The performance of dictionary operations is a bit slow.
Lists are mutable.Dictionaries are also mutable.
Python List vs Dictionary Table

List vs Dictionary: Syntax and Definition

A Python list is a collection object used to store data in a sequential manner. In a list, each element has a particular position and we can access the data randomly using their position. On the other hand, a Python dictionary contains key-value pairs that don’t have a particular position.  

We define a list using the square brackets whereas we use curly braces to create a dictionary. You can observe this in the following example.

myList=[1,2,3,4,5,6]
myDict={0:1,1:2,2:3,3:4,4:5,5:6}

We can also create a list using the list() function. The list() function takes a container object like a set, tuple, etc as its input and returns a list. To create a dictionary, we can use the dict() function. The dict() function takes a list of tuples as its input argument and returns a dictionary. You can observe this in the following example.

myList=list((1,2,3,4,5,6))
myDict={dict([(0,1),(1,2),(2,3),(3,4),(4,5),(5,6)])

We use the index of an element to access an element in a list. In a dictionary, we use the key to access the value as shown below.

myList[0] #0 is position
myDict[0] # 0 is key

Python List vs Dictionary: Mutability

Lists and dictionaries both are mutable. 

We can add elements to a list using the append() and extend() methods. The append() method, when invoked on a list, takes an element as its input and adds it to the list. The extend() method, when invoked on a list, takes a container object and adds all the elements of the container object to the list. You can observe this in the following example.

myList=[1,2,3,4,5,6]
myList.append(10) #output becomes [1,2,3,4,5,6, 10]
myList.extend([10,11,12,13])# output is [1,2,3,4,5,6,10,11,12,13]

To add a key-value pair into a Python dictionary, we can directly assign the key-value pair to the dictionary object using the assignment operator. We can also add elements to a dictionary using the update() method. The update() method, when invoked on a dictionary,  takes another dictionary object as its input. After execution, it adds a key-value pair from the new dictionary to the original dictionary object if the key isn’t already present in the original dictionary object. Otherwise, it updates the values according to the keys in the new dictionary object. You can observe this in the following example.

myDict={0:1,1:2,2:3,3:4,4:5,5:6}
myDict.update({9:81,10:100})#output is {0:1,1:2,2:3,3:4,4:5,5:6,9:81,10:100}

We can also remove elements from a dictionary as well as a list using different methods defined for them. Thus, lists and dictionaries both are mutable. 

List vs Dictionary: Performance

If we consider performance, lists are more efficient than dictionaries most number of times. 

To create a list having 6 elements, the Python interpreter takes 50 nanoseconds. On the contrary, it takes  247 nanoseconds for creating a dictionary with six key-value pairs. You can observe this in the following example.

List vs Dictionary Performance while creation
List vs Dictionary Performance while creation

Accessing an element from a list and a dictionary have almost identical performance. However, the list also wins this battle by a slight margin. You can observe this in the following example.

List vs Dictionary Performance while access
List vs Dictionary Performance while access

Lists perform better than dictionaries while iteration. Just iterating through the elements of a list takes around 108 ns. On the other hand, iterating through the keys of a dictionary with 6 key-value pairs takes around 132 ns. You can observe this in the following example.

List vs Dictionary Performance while iteration
List vs Dictionary Performance while iteration

When to Use List vs Dictionary in Python?

If the values that you want to store have an inherent order and you want random access, you can use a list. When you have key-value pairs that don’t have a particular order, you can use a Python dictionary. 

If you need to remove elements from the program frequently, you should use a dictionary. Python lists have more time complexity than dictionaries while removing an element. 

Conclusion

In this article, we discussed different aspects of Python list vs dictionary to discuss the syntax, definition, Mutability, and performance of both these data structures. To learn more about Python programming, you can read this article on tuple vs list in Python. You might also like this article on for loop vs while loop in Python.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us