if the sequence of the items are important which list will we use
Answers
Background
Lists are an increasingly popular topic for those who are starting to learn Python as well as for those who are already experienced with the language. If we believe the search results in Google Trends, the search interest in this topic has been rising each and every year.
If you are a regular visitor to forums to answer or ask questions about Python programming,you might know the reason behind it.
A lot of Python questions find their ways to these forums and continue to persist there, where users mark some as ‘duplicate’ or ‘sticky’, upvote them or discuss the right solution with others.
With this blog post,datacamp wants to help you to tackle one topic, namely, the most frequently asked questions about lists in Python, and this in an interactive way!
What’s A Python List Exactly?
Lists are one of the four built-in data structures in Python, together with tuples, dictionaries, and sets. They are used to store an ordered collection of items, which might be of different types but usually they aren’t. Commas separate the elements that are contained within a list and enclosed in square brackets. Just like in this example:
script.py
IPython Shell
Run
You see that the second variable biggerZoo is a list that is similar to the zoo variable. However, you also see that biggerZoo contains another list with different types of monkeys within.
Since lists in Python store ordered collections of items or objects, we can say that they are sequence types, precisely because they behave like a sequence. Other types that are also considered to be sequence types are strings and tuples.
You might wonder what’s so special about sequence types. Well, in simple words, it means that the program can iterate over them! This is why lists, strings, tuples, and sets are called “iterables”.
Keep in mind that this concept is particularly important and that it could be that you see this concept returning in other programming languages that are used in data science, such as Scala!
Now we get to the real work! Let’s get deeper into which questions about lists that might have or could haunt you as a Python programmer. Here's a list of all the questions we will answer in this tutorial:
1. When To Use Python Lists And When To Use Tuples, Dictionaries Or Sets
The introduction seems pretty straightforward when you’re just reading it, but when you’re actually working on a small python script or a whole project, the choice for a list or some other sequence type might not be as clear to you.
However, choosing the right data structure for your data is essential!
Keep on reading to find out more.
Lists Versus Tuples
Tuples are used to collect an immutable ordered list of elements. This means that:
You can’t add elements to a tuple. There’s no append() or extend() method for tuples,
You can’t remove elements from a tuple. Tuples have no remove() or pop() method,
You can find elements in a tuple since this doesn’t change the tuple.
You can also use the in operator to check if an element exists in the tuple.
So, if you’re defining a constant set of values and all you’re going to do with it is iterate through it, use a tuple instead of a list. It will be faster than working with lists and also safer, as the tuples contain “write-protect” data.
Lists Versus Dictionaries
A list stores an ordered collection of items, so it keeps some order. Dictionaries don’t have any order.
Dictionaries are known to associate each key with a value, while lists just contain values.
Use a dictionary when you have an unordered set of unique keys that map to values.
Note that, because you have keys and values that link to each other, the performance will be better than lists in cases where you’re checking membership of an element.
Lists Versus Sets
Just like dictionaries, sets have no order in their collection of items. Not like lists.
Set requires the items contained in it to be hashable, lists store non-hashable items.
Sets require your items to be unique and immutable. Duplicates are not allowed in sets, while lists allow for duplicates and are mutable.
You should make use of sets when you have an unordered set of unique, immutable values that are hashable.
Answer:
a backround
Explanation:
becuse it covers the screen