Python, Difference between a list() and dict() dictionary

Python, Difference between a list() and dict() dictionary ?

Python | List and dictionary are fundamentally different types of data in Python and sequences are used to store different types of data in Python.

Divided into two types of categories.

Python list() v/s dict() 

list() usage and example:

To use a python list() if you have an ordered collection of items.

Example: list=["John", "Alex", "Peter"]
list_of_users = list()

dict() Dictionary usage and example:

Use a python dictionary when you have a set of unique keys that map to values.

Example: dict={"1": "John"}
list_of_users = list()
for i in range(5): 
  user_info = dict()
  user_info = {"id": int(i), "username": "user-"+str(i)}
  list_of_users.append(user_info)

print(list_of_users)

Output here:

[
  {'id': 0, 'username': 'user-0'}, 
  {'id': 1, 'username': 'user-1'}, 
  {'id': 2, 'username': 'user-2'}, 
  {'id': 3, 'username': 'user-3'}, 
  {'id': 4, 'username': 'user-4'}
]

Any query? Please share your thoughts in the comments below. Glad to help you!
Thanks!

Was this page helpful? Do not forget to share this post!

Leave a Reply

Your email address will not be published. Required fields are marked *