One of the most basic and easy skills to learn as a new coder is how to create a list in Python. But when you have the option of creating dictionaries – which can be even more powerful – the question is, why do you need this ability!
In this post, we’ll explain how to create a list in Python and when to choose it over a dictionary.
How to create a list in Python and why?
In programming, a list is a variable that contains many. other Variables. These are added to the list in the order in which they can be referenced at any time. In Python, lists can contain multiple data types: strings, integers, booleans, and more.
Therefore, the types of lists you build in Python are often similar to the lists you create in real life: lists of names, lists of phone numbers, lists of locations, etc.
Learning how to create a list in Python is extremely easy. Simply define a variable using square brackets, then separate the elements in the list with commas:
Code
to_do_list = ["Take out trash", "Wish Dad happy birthday", "Answer emails"]
We can then print the items in this list to the screen:
Code
for x in range(0, len(to_do_list)):
print(to_do_list[x])
Or, we can print individual items from that list:
Code
print(toDoList[3])
Code
toDoList[1:2]
(Remember, the second digit is not inclusive, and the first position on any list is indicated as “0”).
At any time, we can now add items to the list or put them in certain places in the list. for instance:
Code
toDoList.append("Defrost chicken")
This will add the item to the end of the list.
If you want to know how to create an empty list in Python, that’s pretty straightforward:
Code
to_do_list = []
Code
to_do_list.insert(2, "Send invoice")
It is now possible to use append and insert to populate the list programmatically. If we want to remove items, we use:
Code
to_do_list.remove(3)
And that’s how to create a list in Python!
If you want to know more about this issue, check out the following article.
How to create a dictionary in Python
A dictionary is similar to a list, but with a key difference. It’s a quote, by the way.
The difference is that instead of being a sequential list that uses an index, a dictionary acts like a database with “keys” to use values as references.
We create dictionaries in the same way as lists, but using curly braces and including both the key and the value. For example, we may store phone numbers such as:
Code
phone_book={"Jeff" : 7701489772,"Bill" : 7378999911, "Nancy" : 7711289354}
Then we can extract a person’s number using their name:
Code
print(phone_book["Jeff"])
For more on using dictionaries, see our detailed article:
When to use lists and vocabulary
So, now you know how to create a list in Python and how to create a dictionary! The question remains: When do you use each? What is the advantage of one over the other?
Let’s start by looking at the obvious limitations of using lists. When we use lists in Python, we create a challenge for ourselves because the index of the list changes every time we add new elements. Therefore, it is very difficult to maintain any kind of system.
For example, if we want to store phone numbers as a list but keep track. whose Each was a number, the only way we could do that was to either store two values in a row (the name followed by the number) or create a second list where each person’s position was stored as their own number.
Also read: How to loop in Python
Although both options can work, it is better to twist them and make them easier to mix.
Moreover, they are dictionaries. as well as Much faster to search data compared to lists!
But is vocabulary always superior? Why do you want to learn how to create a list in Python?
The obvious answer here is to wait for the detailed order. This is important if we want to give detailed orders. And there are many situations when this is useful: when giving relative importance, for example, when creating a task list, the order of steps, etc.
Lists are a little easier to use if you only need to store a few items and are in a hurry. Similarly, while dictionaries are faster, they use more memory compared to lists.
As you can see then, choosing the right format in Python is a matter of knowing your purpose. Now you know how to create a list in Python, and you know when you should!
For more developer news, features and tutorials from Android Authority.Don’t miss out on signing up for our monthly newsletter below!