18 Most Common Python List Questions
18 Most Common Python List Questions
This tutorial will offer simple solutions, easy-to-follow and straightforward explanations and some tips and tricks with Python List Questions that you can practice on the spot with the help of some interactive exercises!
If you want to learn about lists in a course format, you should take a look at our Intro to Python for Data Science course, which has a chapter on pythons lists.
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, such as Stack Overflow, Quora or Reddit, 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:
# These list elements are all of the same type
zoo = [‘bear’, ‘lion’, ‘panda’, ‘zebra’]
print(zoo)
# But these list elements are not
biggerZoo = [‘bear’, ‘lion’, ‘panda’, ‘zebra’,
[‘chimpanzees’, ‘gorillas’, ‘orangutans’, ‘gibbons’]]
print(biggerZoo)
- IPython Shell
In [1]:
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()
orextend()
method for tuples, - You can’t remove elements from a tuple. Tuples have no
remove()
orpop()
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.
You aren’t sure which values are hashable?
Take a look below just to be sure:
Hashable | Non-Hashable |
---|---|
Floats | Dictionaries |
Integers | Sets |
Tuples | Lists |
Strings | |
frozenset() |
Tip: don’t believe us on our word! Try it out for yourself below and experiment in the IPython shell 🙂
# Import the `collections` library
____ ____
# Check if a dictionary is hashable
print(isinstance({}, collections.Hashable))
# Check if a float is hashable
print(isinstance(0.125, collections.Hashable)
In [1]:
Note that, as you’re working with hashable items, checking membership (testing whether a certain element is part of your sequence) will go faster with sets than with lists.
2. How To Select An Element From A List
If you want who work properly with these lists, you will need to know how to access them.
You typically access lists to change certain values, to update or delete them, or to perform some other kind of operation on them. The way you access lists, and, by extension, all other sequence types, is by using the index operator [ ]
. Inside, you put an integer value.
Let me show you an example:
# Select the first list element
oneZooAnimal = biggerZoo[0]
# Print `oneZooAnimal`
print(____)
In [1]:
Run the code and see what you get back from this piece of code. You will notice that the first element of the biggerZoo
variable is returned. It might be a bit confusing or surprising at first, but indices start from 0 and not 1.
How To Get The Last Element Of A List In Your List
The answer to this question is an addition to the explanation in the previous section.
Try putting a negative value, such as -1 or -2 to the index operator to retrieve the last element of our biggerZoo
list!
# Pass -1 to the index operator on `biggerZoo`
monkeys = ___
print(monkeys)
# Pass -2 to the index operator on `biggerZoo`
zebra = ___
print(zebra)
In [1]:
Not too hard, is it?
What Does The Index Out Of Range Error Mean?
This error is one that you will see quite often, especially when you’re new to programming or new to Python.
The best way to understand what this error means is to try it out for yourself.
Use the interactive learning widget above and try passing excessively small negative or large positive numbers!
As you can see, you might get an “Index Out Of Range” error in cases where you pass an integer value to the index operator that is bigger or way smaller than your list! It means that you are assigning a value or referring to an index that does not exist (yet).
This piece is an example of code that will trigger an “Index Out Of Range” error. Let it run and see for yourself!
# Run this code to trigger an “Index Out Of Range” Error
biggerZoo[6]
In [1]:
The Slice Notation In Lists
When you’re new to programming or Python, this can be one particularly confusing topic.
In general, you use the slice notation when you want to select more than one list element at a time. Much like when you choose just one element from a list, you use the double brackets. What’s so special now is that instead of just an integer, you also put a :
in between the double brackets, just like this:
# Use the slice notation like this
someZooAnimals = biggerZoo[2: ]
# Print to see what you exactly select from `biggerZoo`
print(someZooAnimals)
# Try putting 2 on the other side of the colon
otherZooAnimals = biggerZoo[____]
# Print to see what you’re getting back
print(otherZooAnimals)
In [1]:
You see that now we get the biggerZoo
list, but only from the panda
entry at index 2 onwards. In other words, we start from index 2 and go to the end of the list, since no other integer is specified.
What happens now when we do the reverse, you think?
Find out if you’re right by running the code chunk above!
You see that now we only get back bear
and lion
. Now we start from index 0 and go to index 2. Contrary to what you might have expected, the result does not include the panda
entry.
In general, the following holds:
# items start through the end (but the end is not included!)
a[start:end]
# items start through the rest of the array
a[start:]
# items from the beginning through the end (but the end is not included!)
a[:end]
Tip: by just passing a colon :
, you will just copy the list!
In addition to the simple slice notation, there is also the step value, which can be generalized like this:
# start through not past end, by step
a[start:end:step]
So what does this step value exactly do?
Well, it allows you to literally step through your list and select only those elements that your step value includes. An example:
# Print to see how the step value influences your result
print(biggerZoo[2::2])
print(biggerZoo[1::3])
In [1]:
Note that if you don’t specify any step value, it is just set to 1. No elements will then be skipped when stepping through the list.
Remember that your result does not include the end value index that you specify in the slice notation!
How To Randomly Select An Element In A List
You can select a random element from your list with the random
package:
# Import `choice` from the `random` library
from ____ import ____
# Construct your `list` variable with a list of the first
4 letters of the alphabet
list = [‘_’,’_’,’_’,’_’]
# Print your random ‘list’ element
print(choice(list))
In [1]:
If you also want to select a random list element through the indices of your list, you can work with the randrange
method from the random
library
# Import `randrange` from the `random` library
from ____ import ____
# Construct your `randomLetters` variable with a list of
the first 4 letters of the alphabet
randomLetters = [‘_’,’_’, ‘_’, ‘_’]
# Select a random index from ‘randomLetters`
randomIndex = randrange(0,len(randomLetters))
# Print your random element from `random`
print(randomLetters[randomIndex])
In [1]:
Tip: you should consider checking out the random
library, as it will come in handy when you’re programming in Python!
3. How To Transform Python Lists Into Other Data Structures
Sometimes, a list is not exactly what you need. In these cases, you might want to know how to transform your list to a more appropriate data structure. Below, we list some of the most common transformation questions and their answers.
How To Convert A List To A String
You convert a list to a string by using ''.join()
. This operation allows you to glue together all strings in your list together and return them as a string. Read more
# List of Strings to a String
listOfStrings = [‘One’, ‘Two’, ‘Three’]
strOfStrings = ”.join(listOfStrings)
print(strOfStrings)
# List Of Integers to a String
listOfNumbers = [1, 2, 3]
strOfNumbers = ”.join(str(n) for n in listOfNumbers)
print(strOfNumbers)
In [1]:
Note that if your list only contains integers, you should convert the elements to strings before performing the join on them. This is illustrated in the second example in the interactive code block above.
The conversion from an integer to a string is done by looping over the original listOfNumbers
list. If you want to know more about how to loop over a list, go to question 12 and its answer.
How To Convert A List To A Tuple
You can change a list to a tuple in Python by using the tuple()
function. Pass your list to this function, and you will get a tuple back!
Remember: tuples are immutable. You can’t change them afterward!
Tip: you can try transforming your listOfStrings
into a tuple in the interactive exercise of the next section.
How To Convert Your List To A Set In Python
As you will remember, a set is an unordered collection of unique items. That means not only means that any duplicates that you might have had in your original list will be lost once you convert it to a set, but also the order of the list elements.
You can change a list into a set with the set()
function. Just pass your list to it!
Now practice converting your list listOfStrings
into a tuple and a set here:
# Pass your list to `tuple()`
tuple(___)
# Transform your list into a set
set(___)
In [1]:
How To Convert Lists To A Dictionaries
A dictionary works with keys and values, so the conversion from a list to a dictionary might be less straightforward. Let’s say you have a list like this:
helloWorld = ['hello','world','1','2']
You will need to make sure that ‘hello’ and ‘world’ and ‘1’ and ‘2’ are interpreted as key-value pairs. The way to do this is to select them with the slice notation and pass them to zip()
.
zip()
actually works like expected: it zips elements together. In this case, when you zip the helloWorld elements helloWorld[0::2]
and helloWorld[1::2]
, your output will be:
list(zip(helloWorld))
In [1]:
Note that you need to use list()
to print out the result of the zip()
function.
You will pass this to the dict()
function, which will interpret hello
as a key and world
as a value. Similarly, 1
will be interpreted as a key and 2
as a value.
Run the code below to confirm this:
# Convert to a dictionary
helloWorldDictionary = dict(zip(helloWorld[0::2],
helloWorld[1::2]))
# Print out the result
print(helloWorldDictionary)
In [1]:
Note that the second element that is passed to the zip()
function makes use of the step value to make sure that only the world
and 2
elements are selected. Likewise, the first element uses the step value to select hello
and 1
.
If your list is large, you will probably want to do something like this:
a = [1, 2, 3, 4, 5]
# Create a list iterator object
i = iter(a)
# Zip and create a dictionary
print(dict(zip(i, i)))
In [1]:
Note that an iterable object can give you an iterator. The iterator, which has a .__next__
method, holds information on where exactly you are in your iteration: it knows what the next element in the iteration is.
4. How To Determine The Size Of Your List in Python
You can pass your list to the len()
function to get the length of your list back.
Test this out below! We have already made a list of justAList
which you can use.
# Pass `justAList` to `len()`
len(____)
Note that the len()
function is not specifically to retrieve the length of your lists, but can also be used with other sequences or collections, such as dictionaries, sets, strings, etc. You can find more general built-in functions here.
5. What’s The Difference Between The Python append()
and extend()
Methods?
Let’s begin to address this question by revisiting the concept of an iterable that we explained in the first section of this post:
Remember that we say that a certain value is iterable when your program can iterate over it. In other words, an iterable is a value that represents a sequence of one more values. As you probably read in the first section, lists are sequences, and all instances of Python’s sequence types are iterables.
Tip: you check whether a variable is iterable when it has the .__iter__
method, like in this piece of code:
# This is your list
list = [5, 6, 7, 8, 9]
# Check whether it’s iterable
list.__iter_
In [1]:
Run the code above and try out some other simple examples in the IPython shell!
Now that the concept of an iterable is apparent in our minds, we can begin to understand the difference between the two methods. extend()
, on the one hand, takes an iterable (that’s right, it takes a list, set, tuple or string!), and adds each element of the iterable to the list one at a time.
append()
, on the other hand, adds its argument to the end of the list as a single item, which means that when the append()
function takes an iterable as its argument, it will treat it as a single object.
Study the code chunks below and play around with the code for yourself to understand the differences between these two methods
# Append [4,5] to `shortList`
shortList.append([4, 5])
# Use the print() method to show shortList
print(____)
# Extend `longerList` with [4,5]
longerList.extend([4, 5])
# Use the print() method to see longerList
print(____)
In [1]:
You can see that extend()
is a more efficient version of calling append()
multiple times.
6. How To Concatenate Lists in Python
To concatenate lists, you use the +
operator. It will give you a new list that is the concatenation of your two lists without modifying the original ones.
Note that concatenating and appending might be confusing. append()
is different from the +
operator, which in its turn is very similar to the extend()
method, but not exactly the same… You see that with append()
and extend()
, you modify your original list, while with the +
operator, you can make another list variable.
Test this out for yourself and compare with the examples from the previous code chunk:
# Concatenate `shortList` with `[4,5]`
plusList = shortList + [4,5]
#Use the `print()` method to see `plusList`
print(____)
In [1]:
7. How To Sort a List in Python
There are two very simple ways to get the values in your lists sorted in ascending or descending order:
- You use the
sort()
method - Or you use the
sorted()
function and pass a list to it
The sorted()
function can be applied to any Iterable object, which means that it also accepts strings, sets, dictionaries when they are passed to it!
Tip: try this out for yourself! We have already made two lists, rooms
, and orders
, which you can sort for yourself using the two techniques described above :
# Use sort() on the rooms list
____.sort()
# Print out rooms to see the result
print(____)
# Now use the sorted() function on orders
sorted(____)
# Print out orders
print(____)
In [1]:
Note how your original list is changed after you have performed the sorting to it. It is advised to use this in cases where you have no use for your original list anymore.
8. How To Clone Or Copy A List in Python
There are a lot of ways of cloning or copying a list:
- You can slice your original list and store it into a new variable:
newList = oldList[:]
- You can use the built-in
list()
function:newList = list(oldList)
- You can use the
copy
library:- With the
copy()
method:newList = copy.copy(oldList)
- If your list contains objects and you want to copy those as well, you can use
copy.deepcopy()
:copy.deepcopy(oldList)
- With the
Try it out below! We have already created a groceries
list for you to copy and share with your friends.
# Copy the grocery list by slicing and store it in the
`newGroceries` variable
newGroceries = groceries[__]
# Copy the grocery list with the `list()` function and
store it in a `groceriesForFriends` variable
groceriesForFriends = list(____)
# Import the copy library as c
import ____ as __
# Create a `groceriesForFamily` variable and assign
the copied grocery list to it
groceriesForFamily = c.copy(____)
In [1]:
Note that when you use the ‘simple’ copy methods, your original lists will be modified. However, if you use the deepcopy()
method, this will be prevented. Run the code below to see what happens:
# This is your list
objectList = [‘a’,’b’,[‘ab’,’ba’]]
# Copy the `objectList`
copiedList = objectList[:]
# Change the first list element of `copiedList`
copiedList[0] = ‘c’
# Go to the third element (the nested list) and change
the second element
copiedList[2][1] = ‘d’
# Print out the original list to see what happened to
it
In [1]:
For those of you who have already taken lists in Python to the next level, the following section offers you some simple answers to your complex questions.
However, for those of you who wonder what is beyond lists, what advantages libraries might offer instead of the built-in list structure, the next section might certainly come in handy.
Continue to read to find out more!
9. How Does List Comprehension Work In Python?
List comprehension is, basically speaking, a way of elegantly constructing your lists. The best thing about this for those who love math is the fact that they look a lot like mathematical lists. Judge for yourself:
1
[x**2 for x in range(10)]
In [1]:
Now, what does this mean? You take all numbers from 0 to 10, where 10 is not included, and you multiply each and every one of those numbers by 2. Run the piece of code above to see if the result matches your expectations.
Not too hard, is it? Let’s take it up a notch!
Let’s say you want to make the same list as before, but you only want to multiply the numbers that can be divided by 2. Even this is easy with the help of list comprehension:
1
[x**2 for x in range(10) if x%2==0]
In [1]:
You just add a condition, indicated by if
, to your original statement. If that condition proves to be TRUE, the number will be considered for multiplication by 2. In this case, our condition is that the number can be divided by 2 (without having any remainder of the division operation).
Now, in a more general sense, you can also use list comprehension to transform your lists into other lists, which sounds a bit more difficult, doesn’t it?
Consider the next example:
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[(lambda x: x*x)(x) for x in myList]
In [1]:
In this example, you already have a list myList
that you’re transforming through list comprehension: you see that again for x in myList
, for every list element in myList
, some operation will be done.
Next, you see that (lambda x: x*x)
is included. To keep things simple for you, you should just know that lambda functions are anonymous functions that are constructed at runtime. Considering that that piece of code can just serve as an indication for you, you can just ignore it and focus on x*x
.
You see that this function takes one element and multiplies it by itself. It’s an anonymous function, so you need to pass it your list element to make sure that the lambda function takes that one as input. That is why you see (lambda x: x*x)(x)
.
In other words, you could rewrite the whole second line of the above chunk of code as:
f = lambda x: x*x
[f(x) for x in range(10)]
In which you first define your lambda function that says that you want to multiply an element by itself, and then in the second line pass it your list element to make sure that each list element is multiplied by itself.
Note that besides list comprehensions, also set and dictionary comprehensions exist.
This was already quite something, but there is much more to discover about list comprehension.
Keep on reading through the following frequently asked questions to learn more about nested list comprehension and to see more practical examples of what list comprehension can mean for your programming with Python!
10. How To Count Occurrences Of A List Item In Python
Since lists don’t have the item uniqueness criterium, like sets, you might want to know more about how not only many times one particular list item occurs in your list but also how many times each list item is encountered.
Below you will find the answers to both questions!
Counting the occurrences of one item in a list
To count the occurrences of just one list item you can use the count()
method
# Count the occurrences of the number 4
print([1, 2, 9, 4, 5, 4, 1].count(4))
# Count the occurrences of the letter “a”
list = [“d”, “a”, “t”, “a”, “c”, “a”, “m”, “p”]
list.count(“a”)
In [1]:
Counting the occurrences of all items in a list is also known as “tallying” a list, or creating a tally counter.
Counting all items in a list with count()
To count the occurrences of items in a list, you can also use list comprehension in combination with the count()
method
list= [“a”,”b”,”b”]
[[x,list.count(x)] for x in set(list)]
In [1]:
In this piece of code, you first see that your list is converted to a set. This will make sure that only the unique list items are kept, namely a
and b
. Then you say that for each set item, you want to take that item and pass it to a count()
method
Remember that sets only contain unique items!
In this case, first a
will be passed to list.count(x)
, and all occurrences of a
in your original list will be counted.
Counting all list items with Counter()
Alternatively, there’s the faster Counter()
method from the collections
library:
# Import `Counter` from the `collections` library
from collections import Counter
# This is your list
list = [“a”,”b”,”b”]
# Pass `list` to `Counter()`
Counter(list)
In [1]:
Note that Counter()
is generally faster when you want to count all list items.
11. How To Split A Python List Into Evenly Sized Chunks
To split your list up into parts of the same size, you can resort to the zip()
function in combination with iter()
:
# Your list `x`
x = [1,2,3,4,5,6,7,8,9]
# Split `x` up in chunks of 3
y = zip(*[iter(x)]*3)
# Use `list()` to print the result of `zip()`
list(y)
In [1]:
The code above works as follows:
iter()
is an iterator over a sequence.[iter(x)] * 3
produces a list with threelistiterator
objects: each list iterator is an iterator ofx
.- the
*
that is passed to thezip()
function before anything else unpacks the sequence into arguments so that you’re passing the same iterator three times to thezip()
function, and it pulls an item from the iterator each time.
Wait. That last step is totally not clear.
Let me guide you step by step:
- You will have three list iterator objects, which you can think of as:
[1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9]
- The first time,
zip()
will take one element of the list sequentially, which leaves you with:
[1][2][3]
Note that the iterator objects will keep track for you which element is next in the iteration!
- The second time, elements will be added to the three lists you just created, and you will end up with:
[1, 4], [2, 5], [3, 6]
- The last time, you follow the same procedure, and you end up with:
[1, 4, 7], [2, 5, 8], [3, 6, 9]
- Zipping these three lists together will leave you with:
[1, 2, 3], [4, 5, 6], [7, 8, 9]
If, however, you decide to put this logic into a function, you can follow this approach
# Method to split up your lists into chunks
def chunks(list, chunkSize):
“””Yield successive chunkSize-sized chunks from list
.”””
for i in range(0, len(list), chunkSize):
yield list[i:i + chunkSize]
# Use your `chunks` function to print out chunks of the
same size
import pprint
pprint.pprint(list(chunks(range(10, 75), 10)))
In [1]:
Your chunks()
function gets a list and a chunk size as input. It will then print: “Yield successive (your chunk size)-sized chunks from list” (i.e. the list that you give your chunks()
method).
Then, you see range(0, len(list), chunkSize)
: this list starts at 0 and ends at the integer that signals the length of your list. The step value is set equal to the chunkSize
value you pass to chunks()
.
For each element in this list constructed by range()
, you yield a chunk of the original list, starting at the index of the element that it is given and ends at the index + chunk size it is given.
For example, if you index 0 passes through the for loop, a list of [0:2] will be given. In the next iteration, index 2 gets passed through the for loop (remember the step value!), and a chunk of your original list, [2:4] will be outputted.
Note that you use pprint()
or pretty print to print your result.
You can also simply use list comprehension instead of writing a function:
# Set up your list and chunk size
list = range(0, 50)
chunk = 5
# Split up your list into chunks
[list[i:i + chunk] for i in range(0, len(list), chunk)]
In [1]:
12. How To Loop over A List in Python
You can easily loop through any list with a for loop. For those of you who haven’t worked much with for loops, it works as follows: the for loop allows you to perform an action for every element in your list.
In Python, a for loop looks like this:
# This is your list
mylist = [[1,2,3],[4,5,6,7],[8,9,10]]
# Loop over your list and print all elements that are of
size 3
for x in mylist:
if len(x)==3:
print(x)
In [1]:
You have a list mylist
, and you tell Python that you should check if the length of each list element is equal to 3. Since our lists contain two lists that each have three elements in them, namely, [1,2,3]
and [8, 9,10]
, only these will be printed out.
Note how x
in the code chunk above can be changed to element
, listElement
or whatever name you want to give it. Try it out in the console!
You see here that the result doesn’t show the indices of the elements. If you want to iterate over a list by making use of the index, you can do that as follows:
# This is your list
myList = [3,4,5,6]
# Loop over `myList` and print tuples of all indices and
values
for i, val in enumerate(myList):
print(i, val)
In [1]:
Since you want to print out not only the list elements but also their index, you, of course, have to show this in your for loop. This is why we use enumerate(myList)
, which will return an index and a value. Naturally, we have to show this also in the beginning of our for statement: you see i, val
to make clear that we’re expecting an index and a value as a result.
Note that you can also use list comprehension to loop over a list. Here, the results are already printed out without you having to pass print()
1
[x for x in myList if len(x)==3]
In [1]:
13. How To Create Flat Lists Out Of Lists
To make a simple list out of a list of lists, you can use the sum()
function. Just don’t forget to pass your list and an empty list to it
# Your initial list of lists
list = [[1,2],[3,4],[5,6]]
# Flatten out your original list of lists with `sum()`
sum(list, [])
In [1]:
You can also use a two different approaches to flatten your lists:
# You can reduce the lists of lists of lists like this
from functools import reduce
print(reduce(lambda x,y: x+y,listOfLists))
# Or you can use list comprehension
print([item for sublist in listOfLists for item in
sublist])
In [1]:
- You either use the
reduce()
method, to which you pass a lambda function. Now, thereduce()
method is there to ensure that your iterable is reduced to a single value. How it is reduced, you determine with the function that you pass to it. In this case, you say that you want to sum the elements cumulatively: what happens, is that[1,2]
is added to[3,4]
and this result is added to[5,6]
. Represented visually, this gives:
Remember that the +
operator concatenates lists.
- The second option to make flat lists out of lists of lists is to use list comprehension. This nested for loop statement doesn’t make it easy on you. What it comes down to is that you first take
sublist in list
or[1, 2]
,[3, 4]
,[5, 6]
and then you want to consideritem for item in sublist
, printing out each item from the sublist.
Another way of seeing this nested list comprehension is to rewrite it as a regular for loop:
list1 = []
for sublist in list:
for item in sublist:
list1.append(item)
14. How To Get An Intersection Of Two Python Lists
List Intersection Using List Comprehension
If you want to obtain the intersection of two lists, you can use the filter()
function.
This is illustrated in the exercise below. We have already defined two lists for you, list1
and list2
. Use the IPython shell if you want to check the contents of the lists.
# Intersect both lists with list comprehension
intersection = [list(filter(lambda x: x in list1, sublist
)) for sublist in list2]
# Print the result of the intersection
print(____)
In [1]:
In the piece of code above, the filter()
part takes each sublist’s item and checks to see if it is in the source list list1
. The list comprehension is executed for each sublist in list2
.
Note that, since filter()
returns an iterable instead of a list, you need to wrap all you put into your filter()
with list()
. Try removing the list()
to test this out for yourself!
For better readability, you could also work with nested list comprehensions:
# An intersection of both lists, stored in `intersection`
intersection = [[x for x in sublist if x in list1] for
sublist in list2]
# Print the result of the intersection
print(intersection)
In [1]:
Intersecting Lists With set()
If the order of your elements is not important and if you don’t need to worry about duplicates then you can use set intersection
# Make use of the list and set data structures
print(list(set(list1) & set(list2)))
# Use `intersection()`
print(list(set(list1).intersection(list2)))
In [1]:
In the first example, you convert both lists to sets, intersect them and then output them again as a list. The order in the outputted list will be arbitrary. You can also convert the larger of the two lists into a set, after which you can get the intersection of that set with any iterable using intersection()
.
Note that for these two approaches to work, your list should not contain other lists. That is why list2
looks different. Print it out just to make sure!
15. How To Remove Duplicates From A List in Python
As described in the first section of this blog post, it’s best to select a set if you want to store a unique collection of unordered items.
To create a set from any iterable, you can simply pass it to the built-in set()
function. If you later need a real list again, you can similarly pass the set to the list()
function.
Remember that an iterable can be a list, dictionary, … Check whether a variable is an iterable by applying the method .__iter__
to it.
The following example clearly demonstrates the use of set()
to delete all duplicates from your list
# Your list with duplicate values
duplicates = [1, 2, 3, 1, 2, 5, 6, 7, 8]
# Print the unique `duplicates` list
print(list(set(duplicates)))
# A list with small numbers
smallNumbers = [1, 2, 3]
# Print the unique `duplicates` list without the small
numbers
list(set(duplicates) – set(smallNumbers)
In [1]:
As you can see from the last result, the original order of your list is not maintained after you detract the set elements of smallNumbers
. As you already know by now, sets themselves are unordered collections, so any order that you had in your list is lost. When you convert your set back to a list, an arbitrary order is created for you.
If the order of your elements is important to you, then you will have to use a different mechanism. This page covers that topic in more detail.
16. Why NumPy Instead Of Python Lists?
In general, there seem to be four reasons why Python programmers prefer NumPy arrays over lists in Python:
- because NumPy arrays are more compact than lists.
- because access in reading and writing items is faster with NumPy.
- because NumPy can be more convenient to work with, thanks to the fact that you get a lot of vector and matrix operations for free
- because NumPy can be more efficient to work with because they are implemented more efficiently.
17. How To Create Empty NumPy Arrays
If you just want to create an empty NumPy array, you can execute the following
import numpy
numpy.array([])
In [1]:
However, if your goal is to initialize a NumPy array, you can also follow one of the following approaches:
# Make a NumPy array of four rows and two columns and
filled with 0
numpy.zeros(shape=(4,2))
# Make a NumPy array of 1 values of three columns
numpy.ones(3)
# Make an empty NumPy array
numpy.empty(shape=(0,0))
In [1]:
Make sure to check out this documentation if you’re looking for other ways to initialize your NumPy array.
18. How To Do Math With Lists in Python
A lot of the time, we not only use lists to store a collection of values, but we also use it to perform some mathematical operations on it. This section will cover some of the most frequently asked questions on doing math with lists.
How To Calculate the Weighted Average of a List
The weighted average is much like an average but is slightly different: a weighted average returns a number that depends on the variables of both value and weight.
This definition might seem a bit heavy at first. Let’s take a look at an example:
cost = [0.424, 0.4221, 0.4185, 0.4132, 0.413]
cases = [10, 20, 30, 40, 50]
You can easily calculate the weighted average with the following pieces of code
for c in range(len(cost)):
cost[c] = (cost[c] * cases[c] / sum(cases))
cost = sum(cost)
print(cost)
In [1]:
But, you can also go for one of these two other approaches. Firstly, you can use:
sum(cost[c] * cases[c] / sum(cases) for c in range(len
(cost)))
In [1]:
Or you can use this one:
sum(cost[c] * cases[c] for c in range(len(cost))) / sum
(cases)
In [1]:
Lastly, you can also calculate the weighted average of your list with the help of the zip()
function.
Tip: make a variable costAndCases
to which you just assign the zip()
function, with the cost
and case
variables assigned to it. You will see how another list is created with only tuples. The first value of each tuple expresses the cost, while the second element expresses the cases that can be bought for that price.
# See what `zip()` does to your `cost` and `cases`
print(list(zip(____, ____)))
# Calculate the weighted average
print(sum([x * y for x, y in zip(cost, cases)]) / sum
(cases))
In [1]:
Remember what zip()
does to your lists? Easy, it zips your lists together, literally. For example, zip(cost, cases)
would output [(0.424, 10), (0.4221, 20), (0.4185, 30), (0.4132, 40), (0.413, 50)]
.
How To Calculate the Quantile of a List in Python
Quantiles are essential to making a summary of your data set, next to the minimum and maximum of your set. You also have the 25th, 50th, and the 75th percentile, but they are also called first quartile, median and third quartile.
That means that in total, you need five numbers to summarize your entire data set: the minimum, maximum, the median and your two quartiles.
The minimum and the maximum of your data set are easy to find, but what about those percentiles?
It’s easy. Let’s say you have 25 instances, sorted from low to high, in your data set:
- The 25th percentile or first quartile is calculated by multiplying 0.25 with 25. The resulting number, 6.25 or 7 if we round up, gives you an indication which number is the 25th percentile: the 7th instance in your sorted list of instances.
- The third quartile or 75th percentile is calculated by multiplying 0.75 with 25. The resulting number, 18.75, 19 if we round up, tells you that the 19th instance in your sorted list of instances is your third percentile.
- The median of your data set is calculated by multiplying 0.5 with 25. The 13th number will be the median since you round up 12.5.
But how do you program this to work for your list?
The easiest way to do this is by working with NumPy. Follow the instructions in the interactive exercise below to calculate the percentiles or your list:
# Import numpy as np
import ____ as __
# Make a NumPy array
a = np.array([1,2,3,4,5])
# Return the 50th percentile of our NumPy array
p = np.percentile(a, 50)
#Print the result
print(____)
In [1]:
How To Sum Lists Element-Wise
Let’s say you have two lists:
list1=[1, 2, 3]
list2=[4, 5, 6]
And you want to make sure that you add the list element element-wise; That is, that 1 is added to 4, 2 to 5 and 3 to 6. You have two ways of accomplishing this.
Keep on reading to learn more!
… With Basic Python
You can use map with operator.add
to perform an element-wise additio
from operator import add
list(map(add, list1, list2))
In [1]:
This will result in a list [5, 7, 9]
.
Note how you will need to make use of list()
to print the result of the map()
function.
Or you can use the zip()
function with a list comprehension
[sum(x) for x in zip(list1, list2)]
In [1]:
… With NumPy
The previous examples worked perfectly on these small lists, but when you’re working with slightly more data, you should probably use numpy
to make the process easier.
You should then make sure that you import the numpy
library and that your lists are converted into NumPy arrays
# Import numpy as np
import ____ as __
# Make your lists into NumPy arrays
vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5, 6])
# Element-wise addition
sum_vector = vector1 + vector2
# Print the result
print(____)
In [1]:
What’s Up Next In Your Data Science Journey With Python?
Lists are only a small but essential element in your data science journey with Python. There’s so much more to explore! Our interactive Intro to Python for Data Science course is perfect for those who want to learn If more about Python and how it is used in data science: it will start from the very basics of Python to gradually and gently bring you to an intermediate level by introducing NumPy to you. Perfect for those with little experience in (Python) programming!
If, however, you’re already a more advanced Python user, you might just consider checking out our Intermediate Python For Data Science course, tailored to those who are ready to take their data science and Python skills to the next level. This course will definitely bring you even further in your data science learning! The course introduces you to more libraries, such as matplotlib and pandas, concepts, such as logic, control flow, and filtering, and includes a data science case study in order to consolidate your skills.
Are you already using Python for data science but you want to go even further? You might be interested in our Importing Data Into Python course or consider reading our The importance of preprocessing in data science and the machine learning pipeline tutorial series!
——————————————————————————————————–
python list comprehension, python list append, python list to string, python list length, python list files in directory, python list sort, python list remove, python list index, python list