Parallel Assignment In Python Do I Always Have To Type

Variables¶

Recall that a variable is a label for a location in memory. It can be used to hold a value. In statically typed languages, variables have predetermined types, and a variable can only be used to hold values of that type. In Python, we may reuse the same variable to store values of any type.

A variable is similar to the memory functionality found in most calculators, in that it holds one value which can be retrieved many times, and that storing a new value erases the old. A variable differs from a calculator’s memory in that one can have many variables storing different values, and that each variable is referred to by name.

Defining variables¶

To define a new variable in Python, we simply assign a value to a label. For example, this is how we create a variable called , which contains an integer value of zero:

This is exactly the same syntax as assigning a new value to an existing variable called . Later in this chapter we will discuss under what circumstances this statement will cause a new variable to be created.

If we try to access the value of a variable which hasn’t been defined anywhere yet, the interpreter will exit with a name error.

We can define several variables in one line, but this is usually considered bad style:

In keeping with good programming style, we should make use of meaningful names for variables.

# Define three variables at once:count,result,total=0,0,0# This is equivalent to:count=0result=0total=0

Variable scope and lifetime¶

Not all variables are accessible from all parts of our program, and not all variables exist for the same amount of time. Where a variable is accessible and how long it exists depend on how it is defined. We call the part of a program where a variable is accessible its scope, and the duration for which the variable exists its lifetime.

A variable which is defined in the main body of a file is called a global variable. It will be visible throughout the file, and also inside any file which imports that file. Global variables can have unintended consequences because of their wide-ranging effects – that is why we should almost never use them. Only objects which are intended to be used globally, like functions and classes, should be put in the global namespace.

A variable which is defined inside a function is local to that function. It is accessible from the point at which it is defined until the end of the function, and exists for as long as the function is executing. The parameter names in the function definition behave like local variables, but they contain the values that we pass into the function when we call it. When we use the assignment operator () inside a function, its default behaviour is to create a new local variable – unless a variable with the same name is already defined in the local scope.

Here is an example of variables in different scopes:

Note

The inside of a class body is also a new local variable scope. Variables which are defined in the class body (but outside any class method) are called class attributes. They can be referenced by their bare names within the same scope, but they can also be accessed from outside this scope if we use the attribute access operator () on a class or an instance (an object which uses that class as its type). An attribute can also be set explicitly on an instance or class from inside a method. Attributes set on instances are called instance attributes. Class attributes are shared between all instances of a class, but each instance has its own separate instance attributes. We will look at this in greater detail in the chapter about classes.

# This is a global variablea=0ifa==0:# This is still a global variableb=1defmy_function(c):# this is a local variabled=3print(c)print(d)# Now we call the function, passing the value 7 as the first and only parametermy_function(7)# a and b still existprint(a)print(b)# c and d don't exist anymore -- these statements will give us name errors!print(c)print(d)

The assignment operator¶

As we saw in the previous sections, the assignment operator in Python is a single equals sign (). This operator assigns the value on the right hand side to the variable on the left hand side, sometimes creating the variable first. If the right hand side is an expression (such as an arithmetic expression), it will be evaluated before the assignment occurs. Here are a few examples:

The last statement might look a bit strange if we were to interpret as a mathematical equals sign – clearly a number cannot be equal to the same number plus one! Remember that is an assignment operator – this statement is assigning a new value to the variable which is equal to the old value of plus one.

Assigning an initial value to variable is called initialising the variable. In some languages defining a variable can be done in a separate step before the first value assignment. It is thus possible in those languages for a variable to be defined but not have a value – which could lead to errors or unexpected behaviour if we try to use the value before it has been assigned. In Python a variable is defined and assigned a value in a single step, so we will almost never encounter situations like this.

The left hand side of the assignment statement must be a valid target:

An assignment statement may have multiple targets separated by equals signs. The expression on the right hand side of the last equals sign will be assigned to all the targets. All the targets must be valid:

a_number=5# a_number becomes 5a_number=total# a_number becomes the value of totala_number=total+5# a_number becomes the value of total + 5a_number=a_number+1# a_number becomes the value of a_number + 1
# this is fine:a=3# these are all illegal:3=43=aa+b=3
# both a and b will be set to zero:a=b=0# this is illegal, because we can't set 0 to b:a=0=b

Compound assignment operators¶

We have already seen that we can assign the result of an arithmetic expression to a variable:

Counting is something that is done often in a program. For example, we might want to keep count of how many times a certain event occurs by using a variable called . We would initialise this variable to zero and add one to it every time the event occurs. We would perform the addition with this statement:

This is in fact a very common operation. Python has a shorthand operator, , which lets us express it more cleanly, without having to write the name of the variable twice:

There is a similar operator, , which lets us decrement numbers:

Other common compound assignment operators are given in the table below:

OperatorExampleEquivalent to
# These statements mean exactly the same thing:count=count+1count+=1# We can increment a variable by any number we like.count+=2count+=7count+=a+b
# These statements mean exactly the same thing:count=count-3count-=3

More about scope: crossing boundaries¶

What if we want to access a global variable from inside a function? It is possible, but doing so comes with a few caveats:

The print statement will output , the value of the global variable , as you probably expected. But what about this program?

When we call the function, the print statement inside outputs – but why does the print statement at the end of the program output ?

By default, the assignment statement creates variables in the local scope. So the assignment inside the function does not modify the global variable – it creates a new local variable called , and assigns the value to that variable. The first print statement outputs the value of the new local variable – because if a local variable has the same name as a global variable the local variable will always take precedence. The last print statement prints out the global variable, which has remained unchanged.

What if we really want to modify a global variable from inside a function? We can use the keyword:

We may not refer to both a global variable and a local variable by the same name inside the same function. This program will give us an error:

Because we haven’t declared to be global, the assignment in the second line of the function will create a local variable . This means that we can’t refer to the global variable elsewhere in the function, even before this line! The first print statement now refers to the local variable – but this variable doesn’t have a value in the first line, because we haven’t assigned it yet!

Note that it is usually very bad practice to access global variables from inside functions, and even worse practice to modify them. This makes it difficult to arrange our program into logically encapsulated parts which do not affect each other in unexpected ways. If a function needs to access some external value, we should pass the value into the function as a parameter. If the function is a method of an object, it is sometimes appropriate to make the value an attribute of the same object – we will discuss this in the chapter about object orientation.

Note

There is also a keyword in Python – when we nest a function inside another function, it allows us to modify a variable in the outer function from inside the inner function (or, if the function is nested multiple times, a variable in one of the outer functions). If we use the keyword, the assignment statement will create the variable in the global scope if it does not exist already. If we use the keyword, however, the variable must be defined, because it is impossible for Python to determine in which scope it should be created.

a=0defmy_function():print(a)my_function()
a=0defmy_function():a=3print(a)my_function()print(a)
a=0defmy_function():globalaa=3print(a)my_function()print(a)
a=0defmy_function():print(a)a=3print(a)my_function()

Exercise 1¶

  1. Describe the scope of the variables , , and in this example:

    defmy_function(a):b=a-2returnbc=3ifc>2:d=my_function(5)print(d)
  2. What is the lifetime of these variables? When will they be created and destroyed?

  3. Can you guess what would happen if we were to assign a value of instead?

  4. Why would this be a problem? Can you think of a way to avoid it?

Modifying values¶

Constants¶

In some languages, it is possible to define special variables which can be assigned a value only once – once their values have been set, they cannot be changed. We call these kinds of variables constants. Python does not allow us to set such a restriction on variables, but there is a widely used convention for marking certain variables to indicate that their values are not meant to change: we write their names in all caps, with underscores separating words:

Why do we bother defining variables that we don’t intend to change? Consider this example:

There are several good reasons to define instead of just writing inside the print statement. First, this gives the number a descriptive label which explains what it is – this makes the code more understandable. Second, we may eventually need to refer to this number in our program more than once. If we ever need to update our code with a new value for the maximum mark, we will only have to change it in one place, instead of finding every place where it is used – such replacements are often error-prone.

Literal numbers scattered throughout a program are known as “magic numbers” – using them is considered poor coding style. This does not apply to small numbers which are considered self-explanatory – it’s easy to understand why a total is initialised to zero or incremented by one.

Sometimes we want to use a variable to distinguish between several discrete options. It is useful to refer to the option values using constants instead of using them directly if the values themselves have no intrinsic meaning:

In the above example, the values , and are not important – they are completely meaningless. We could equally well use , and or the strings , and . The only important thing is that the three values must be different. If we used the numbers directly instead of the constants the program would be much more confusing to read. Using meaningful strings would make the code more readable, but we could accidentally make a spelling mistake while setting one of the values and not notice – if we mistype the name of one of the constants we are more likely to get an error straight away.

Some Python libraries define common constants for our convenience, for example:

Note that many built-in constants don’t follow the all-caps naming convention.

# These variables are "constants" by convention:NUMBER_OF_DAYS_IN_A_WEEK=7NUMBER_OF_MONTHS_IN_A_YEAR=12# Nothing is actually stopping us from redefining them...NUMBER_OF_DAYS_IN_A_WEEK=8# ...but it's probably not a good idea.
MAXIMUM_MARK=80tom_mark=58print(("Tom's mark is %.2f%%"%(tom_mark/MAXIMUM_MARK*100)))# %% is how we escape a literal % inside a string
# We define some optionsLOWER,UPPER,CAPITAL=1,2,3name="jane"# We use our constants when assigning these values...print_style=UPPER# ...and when checking them:ifprint_style==LOWER:print(name.lower())elifprint_style==UPPER:print(name.upper())elifprint_style==CAPITAL:print(name.capitalize())else:# Nothing prevents us from accidentally setting print_style to 4, 90 or# "spoon", so we put in this fallback just in case:print("Unknown style option!")
# we need to import these libraries before we use themimportstringimportmathimportre# All the lowercase ASCII letters: 'abcdefghijklmnopqrstuvwxyz'print(string.ascii_lowercase)# The mathematical constants pi and e, both floating-point numbersprint(math.pi)# ratio of circumference of a circle to its diameterprint(math.e)# natural base of logarithms# This integer is an option which we can pass to functions in the re# (regular expression) library.print(re.IGNORECASE)

Mutable and immutable types¶

Some values in python can be modified, and some cannot. This does not ever mean that we can’t change the value of a variable – but if a variable contains a value of an immutable type, we can only assign it a new value. We cannot alter the existing value in any way.

Integers, floating-point numbers and strings are all immutable types – in all the previous examples, when we changed the values of existing variables we used the assignment operator to assign them new values:

Even this operator doesn’t modify the value of in-place – it also assigns a new value:

We haven’t encountered any mutable types yet, but we will use them extensively in later chapters. Lists and dictionaries are mutable, and so are most objects that we are likely to write ourselves:

a=3a=2b="jane"b="bob"
# this is a list of numbersmy_list=[1,2,3]my_list[0]=5# we can change just the first element of the listprint(my_list)classMyClass(object):pass# this is a very silly class# Now we make a very simple object using our class as a typemy_object=MyClass()# We can change the values of attributes on the objectmy_object.some_property=42

More about input¶

In the earlier sections of this unit we learned how to make a program display a message using the function or read a string value from the user using the function. What if we want the user to input numbers or other types of variables? We still use the function, but we must convert the string values returned by to the types that we want. Here is a simple example:

is a function which converts values of various types to ints. We will discuss type conversion in greater detail in the next section, but for now it is important to know that will not be able to convert a string to an integer if it contains anything except digits. The program above will exit with an error if the user enters , or even . When we write a program which relies on user input, which can be incorrect, we need to add some safeguards so that we can recover if the user makes a mistake. For example, we can detect if the user entered bad input and exit with a nicer error message:

This program will still only attempt to read in the input once, and exit if it is incorrect. If we want to keep asking the user for input until it is correct, we can do something like this:

We will learn more about boolean values, loops and exceptions later.

height=int(input("Enter height of rectangle: "))width=int(input("Enter width of rectangle: "))print("The area of the rectangle is %d"%(width*height))
try:height=int(input("Enter height of rectangle: "))width=int(input("Enter width of rectangle: "))exceptValueErrorase:# if a value error occurs, we will skip to this pointprint("Error reading height and width: %s"%e)
correct_input=False# this is a boolean value -- it can be either true or false.whilenotcorrect_input:# this is a while looptry:height=int(input("Enter height of rectangle: "))width=int(input("Enter width of rectangle: "))exceptValueError:print("Please enter valid integers for the height and width.")else:# this will be executed if there is no value errorcorrect_input=True

Example: calculating petrol consumption of a car¶

In this example, we will write a simple program which asks the user for the distance travelled by a car, and the monetary value of the petrol that was used to cover that distance. From this information, together with the price per litre of petrol, the program will calculate the efficiency of the car, both in litres per 100 kilometres and and kilometres per litre.

First we will define the petrol price as a constant at the top. This will make it easy for us to update the price when it changes on the first Wednesday of every month:

When the program starts,we want to print out a welcome message:

Ask the user for his or her name:

Ask the user for the distance travelled:

Then ask the user for the amount paid:

Now we will do the calculations:

Finally, we output the results:

PETROL_PRICE_PER_LITRE=4.50
print("*** Welcome to the fuel efficiency calculator! ***\n")# we add an extra blank line after the message with \n
name=input("Enter your name: ")
# float is a function which converts values to floating-point numbers.distance_travelled=float(input("Enter distance travelled in km: "))
amount_paid=float(input("Enter monetary value of fuel bought for the trip: R"))
fuel_consumed=amount_paid/PETROL_PRICE_PER_LITREefficiency_l_per_100_km=fuel_consumed/distance_travelled*100efficiency_km_per_l=distance_travelled/fuel_consumed
print("Hi, %s!"%name)print("Your car's efficiency is %.2f litres per 100 km."%efficiency_l_per_100_km)print("This means that you can travel %.2f km on a litre of petrol."%efficiency_km_per_l)# we add an extra blank line before the message with \nprint("\nThanks for using the program.")

Exercise 2¶

  1. Write a Python program to convert a temperature given in degrees Fahrenheit to its equivalent in degrees Celsius. You can assume that T_c = (5/9) x (T_f - 32), where T_c is the temperature in °C and T_f is the temperature in °F. Your program should ask the user for an input value, and print the output. The input and output values should be floating-point numbers.
  2. What could make this program crash? What would we need to do to handle this situation more gracefully?

Type conversion¶

As we write more programs, we will often find that we need to convert data from one type to another, for example from a string to an integer or from an integer to a floating-point number. There are two kinds of type conversions in Python: implicit and explicit conversions.

Implicit conversion¶

Recall from the section about floating-point operators that we can arbitrarily combine integers and floating-point numbers in an arithmetic expression – and that the result of any such expression will always be a floating-point number. This is because Python will convert the integers to floating-point numbers before evaluating the expression. This is an implicit conversion – we don’t have to convert anything ourselves. There is usually no loss of precision when an integer is converted to a floating-point number.

For example, the integer will automatically be converted to a floating-point number in the following example:

is a while is an . Python will automatically convert operands so that they are of the same type. In this case this is achieved if the integer is converted to the floating-point equivalent . Then the two floating-point numbers can be multiplied.

Let’s have a look at a more complex example:

Python performs operations according to the order of precedence, and decides whether a conversion is needed on a per-operation basis. In our example has the highest precedence, so it will be processed first. and are both integers and is the integer division operator – the result of this operation is the integer . Now we are left with . The addition and subtraction are at the same level of precedence, so they are evaluated left-to-right, starting with addition. First is converted to the floating-point number , and the two floating-point numbers are added, which leaves us with . The result of this floating-point subtraction is , which is assigned to .

result=8.5+7//3-2.5

Explicit conversion¶

Converting numbers from to will result in a loss of precision. For example, try to convert to an – it is not possible to do this without losing precision. In order for this to happen, we must explicitly tell Python that we are aware that precision will be lost. For example, we need to tell the compiler to convert a to an like this:

The function converts a to an by discarding the fractional part – it will always round down! If we want more control over the way in which the number is rounded, we will need to use a different function:

Explicit conversion is sometimes also called casting – we may read about a being cast to or vice-versa.

# the floor and ceil functions are in the math moduleimportmath# ceil returns the closest integer greater than or equal to the number# (so it always rounds up)i=math.ceil(5.834)# floor returns the closest integer less than or equal to the number# (so it always rounds down)i=math.floor(5.834)# round returns the closest integer to the number# (so it rounds up or down)# Note that this is a built-in function -- we don't need to import math to use it.i=round(5.834)

Converting to and from strings¶

As we saw in the earlier sections, Python seldom performs implicit conversions to and from – we usually have to convert values explicitly. If we pass a single number (or any other value) to the function, it will be converted to a string automatically – but if we try to add a number and a string, we will get an error:

To convert numbers to strings, we can use string formatting – this is usually the cleanest and most readable way to insert multiple values into a message. If we want to convert a single number to a string, we can also use the function explicitly:

# This is OKprint(5)print(6.7)# This is not OKprint("3"+4)# Do you mean this...print("3%d"%4)# concatenate "3" and "4" to get "34"# Or this?print(int("3")+4)# add 3 and 4 to get 7
# These lines will do the same thingprint("3%d"%4)print("3"+str(4))

More about conversions¶

In Python, functions like , and will try to convert anything to their respective types – for example, we can use the function to convert strings to integers or to convert floating-point numbers to integers. Note that although can convert a float to an integer it can’t convert a string containing a float to an integer directly!

Values of type can contain the value or . These values are used extensively in conditional statements, which execute or do not execute parts of our program depending on some binary condition:

The condition is often an expression which evaluates to a boolean value:

However, almost any value can implicitly be converted to a boolean if it is used in a statement like this:

This usually behaves in the way that you would expect: non-zero numbers are values and zero is . However, we need to be careful when using strings – the empty string is treated as , but any other string is – even and !

# This is OKint("3")# This is OKint(3.7)# This is not OKint("3.7")# This is a string representation of a float, not an integer!# We have to convert the string to a float firstint(float("3.7"))
my_flag=Trueifmy_flag:print("Hello!")
if3>4:print("This will not be printed.")
my_number=3ifmy_number:print("My number is non-zero!")
# bool is a function which converts values to booleansbool(34)# Truebool(0)# Falsebool(1)# Truebool("")# Falsebool("Jane")# Truebool("0")# True!bool("False")# Also True!

Exercise 3¶

  1. Convert to a float.
  2. Convert to an integer (with rounding).
  3. Convert to an integer (with rounding).
  4. Convert to a string.
  5. Convert to a string.
  6. Convert to a float.
  7. Convert to a boolean.

Answers to exercises¶

Answer to exercise 1¶

  1. is a local variable in the scope of because it is an argument name. is also a local variable inside , because it is assigned a value inside . and are both global variables. It doesn’t matter that is created inside an block, because the inside of an block is not a new scope – everything inside the block is part of the same scope as the outside (in this case the global scope). Only function definitions (which start with ) and class definitions (which start with ) indicate the start of a new level of scope.
  2. Both and will be created every time is called and destroyed when has finished executing. is created when it is assigned the value , and exists for the remainder of the program’s execution. is created inside the block (when it is assigned the value which is returned from the function), and also exists for the remainder of the program’s execution.
  3. As we will learn in the next chapter, blocks are executed conditionally. If were not greater than in this program, the block would not be executed, and if that were to happen the variable would never be created.
  4. We may use the variable later in the code, assuming that it always exists, and have our program crash unexpectedly if it doesn’t. It is considered poor coding practice to allow a variable to be defined or undefined depending on the outcome of a conditional statement. It is better to ensure that is always defined, no matter what – for example, by assigning it some default value at the start. It is much easier and cleaner to check if a variable has the default value than to check whether it exists at all.

Answer to exercise 2¶

  1. Here is an example program:

    Note

    The formatting symbol is used with floats, and instructs Python to pick a sensible human-readable way to display the float.

    T_f=float(input("Please enter a temperature in °F: "))T_c=(5/9)*(T_f-32)print("%g°F = %g°C"%(T_f,T_c))
  2. The program could crash if the user enters a value which cannot be converted to a floating-point number. We would need to add some kind of error checking to make sure that this doesn’t happen – for example, by storing the string value and checking its contents. If we find that the entered value is invalid, we can either print an error message and exit or keep prompting the user for input until valid input is entered.

Answer to exercise 3¶

Here are example answers:

importmatha_1=float("8.8")a_2=math.round(8.8)a_3=math.round("8.8")a_4="%g"%8.8a_5="%d"%8a_6=float(8)a_7=bool(8)

The post will offer simple solutions, easy-to-follow and straightforward explanations and some tips and tricks 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 free course Intro to Python for Data Science, 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. The elements that are contained within a list are separated by commas and enclosed in square brackets. Just like in this example:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgVGhlc2UgbGlzdCBlbGVtZW50cyBhcmUgYWxsIG9mIHRoZSBzYW1lIHR5cGVcbnpvbyA9IFsnYmVhcicsICdsaW9uJywgJ3BhbmRhJywgJ3plYnJhJ11cbnByaW50KHpvbylcblxuIyBCdXQgdGhlc2UgbGlzdCBlbGVtZW50cyBhcmUgbm90XG5iaWdnZXJab28gPSBbJ2JlYXInLCAnbGlvbicsICdwYW5kYScsICd6ZWJyYScsIFsnY2hpbXBhbnplZXMnLCAnZ29yaWxsYXMnLCAnb3Jhbmd1dGFucycsICdnaWJib25zJ11dXG5wcmludChiaWdnZXJab28pIn0=



You see that the second variable is a list that is similar to the variable. However, you also see that 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, exactly 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 or method for tuples,
  • You can’t remove elements from a tuple. Tuples have no or method,
  • You can find elements in a tuple since this doesn’t change the tuple.
  • You can also use the 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:

FloatsDictionaries
IntegersSets
TuplesLists
Strings

Tip: don’t believe us on our word! Try it out for yourself below and experiment in the IPython shell :)

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZ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AgICBcInByaW50XCIsXG4gICAgbm90X2NhbGxlZF9tc2c9cHJlZGVmX21zZyxcbiAgICBpbmNvcnJlY3RfbXNnPXByZWRlZl9tc2csXG4gICAgZG9fZXZhbD1GYWxzZVxuKVxuc3VjY2Vzc19tc2coXCJBd2Vzb21lIGpvYiFcIikifQ==



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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiYmlnZ2VyWm9vID0gWydiZWFyJywgJ2xpb24nLCAncGFuZGEnLCAnemVicmEnLCBbJ2NoaW1wYW56ZWVzJywgJ2dvcmlsbGFzJywgJ29yYW5ndXRhbnMnLCAnZ2liYm9ucyddXSIsInNhbXBsZSI6IiMgU2VsZWN0IHRoZSBmaXJzdCBsaXN0IGVsZW1lbnRcbm9uZVpvb0FuaW1hbCA9IGJpZ2dlclpvb1swXVxuXG4jIFByaW50IGBvbmVab29BbmltYWxgXG5wcmludChfX19fKSIsInNvbHV0aW9uIjoiIyBTZWxlY3QgdGhlIGZpcnN0IGxpc3QgZWxlbWVudFxub25lWm9vQW5pbWFsID0gYmlnZ2VyWm9vWzBdXG5cbiMgUHJpbnQgYG9uZVpvb0FuaW1hbGBcbnByaW50KG9uZVpvb0FuaW1hbCkiLCJzY3QiOiJwcmVkZWZfbXNnID0gXCJEaWQgeW91IHByaW50IGBvbmVab29BbmltYWw/YFwiXG4jIFRlc3Qgb25lWm9vQW5pbWFsXG50ZXN0X29iamVjdChcIm9uZVpvb0FuaW1hbFwiLCB1bmRlZmluZWRfbXNnID0gcHJlZGVmX21zZywgaW5jb3JyZWN0X21zZyA9IHByZWRlZl9tc2cpXG4jVGVzdCBwcmludFxudGVzdF9mdW5jdGlvbihcbiAgICBcInByaW50XCIsXG4gICAgbm90X2NhbGxlZF9tc2c9cHJlZGVmX21zZyxcbiAgICBpbmNvcnJlY3RfbXNnPXByZWRlZl9tc2csXG4gICAgZG9fZXZhbD1GYWxzZVxuKSJ9



Run the code and see what you get back from this piece of code. You will see that the first element of the 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 list!

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiYmlnZ2VyWm9vID0gWydiZWFyJywgJ2xpb24nLCAncGFuZGEnLCAnemVicmEnLCBbJ2NoaW1wYW56ZWVzJywgJ2dvcmlsbGFzJywgJ29yYW5ndXRhbnMnLCAnZ2liYm9ucyddXSIsInNhbXBsZSI6IiMgUGFzcyAtMSB0byB0aGUgaW5kZXggb3BlcmF0b3Igb24gYGJpZ2dlclpvb2Bcbm1vbmtleXMgPSBfX19cbnByaW50KG1vbmtleXMpXG5cbiMgUGFzcyAtMiB0byB0aGUgaW5kZXggb3BlcmF0b3Igb24gYGJpZ2dlclpvb2BcbnplYnJhID0gX19fXG5wcmludCh6ZWJyYSkiLCJzb2x1dGlvbiI6IiMgUGFzcyAtMSB0byB0aGUgaW5kZXggb3BlcmF0b3Igb24gYmlnZ2VyWm9vXG5tb25rZXlzID0gYmlnZ2VyWm9vWy0xXVxucHJpbnQobW9ua2V5cylcblxuIyBQYXNzIC0yIHRvIHRoZSBpbmRleCBvcGVyYXRvciBvbiBiaWdnZXJab29cbnplYnJhID0gYmlnZ2VyWm9vWy0yXVxucHJpbnQoemVicmEpIiwic2N0IjoicHJlZGVmX21zZyA9IFwiTG9vayBhdCB0aGUgcHJldmlvdXMgZXhhbXBsZSB0byBzZWUgaG93IHlvdSBjYW4gY29uc3RydWN0IHRoZSBpbmRleCBvcGVyYXRvciBvbiBgYmlnZ2VyWm9vXCJcbnplYnJhX3ByaW50X21zZyA9IFwiRGlkIHlvdSBwcmludCBgemVicmFgP1wiXG4jIFRlc3Qgb2JqZWN0c1xudGVzdF9vYmplY3QoXCJtb25rZXlzXCIsIHVuZGVmaW5lZF9tc2cgPSBwcmVkZWZfbXNnLCBpbmNvcnJlY3RfbXNnID0gcHJlZGVmX21zZylcbnRlc3Rfb2JqZWN0KFwiemVicmFcIiwgdW5kZWZpbmVkX21zZyA9IHByZWRlZl9tc2csIGluY29ycmVjdF9tc2cgPSBwcmVkZWZfbXNnKVxuI1Rlc3QgcHJpbnRcbnRlc3RfZnVuY3Rpb24oXG4gICAgXCJwcmludFwiLFxuICAgIG5vdF9jYWxsZWRfbXNnPXByZWRlZl9tc2csXG4gICAgaW5jb3JyZWN0X21zZz16ZWJyYV9wcmludF9tc2csXG4gICAgZG9fZXZhbD1GYWxzZVxuKVxuc3VjY2Vzc19tc2coXCJLZWVwIHVwIHRoZSBnb29kIHdvcmshXCIpIn0=



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!

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiYmlnZ2VyWm9vID0gWydiZWFyJywgJ2xpb24nLCAncGFuZGEnLCAnemVicmEnLCBbJ2NoaW1wYW56ZWVzJywgJ2dvcmlsbGFzJywgJ29yYW5ndXRhbnMnLCAnZ2liYm9ucyddXSIsInNhbXBsZSI6IiMgUnVuIHRoaXMgY29kZSB0byB0cmlnZ2VyIGFuIFwiSW5kZXggT3V0IE9mIFJhbmdlXCIgRXJyb3JcbmJpZ2dlclpvb1s2XSJ9



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 select 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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiYmlnZ2VyWm9vID0gWydiZWFyJywgJ2xpb24nLCAncGFuZGEnLCAnemVicmEnLCBbJ2NoaW1wYW56ZWVzJywgJ2dvcmlsbGFzJywgJ29yYW5ndXRhbnMnLCAnZ2liYm9ucyddXSIsInNhbXBsZSI6IiMgVXNlIHRoZSBzbGljZSBub3RhdGlvbiBsaWtlIHRoaXNcbnNvbWVab29BbmltYWxzID0gYmlnZ2VyWm9vWzI6IF1cblxuIyBQcmludCB0byBzZWUgd2hhdCB5b3UgZXhhY3RseSBzZWxlY3QgZnJvbSBgYmlnZ2VyWm9vYFxucHJpbnQoc29tZVpvb0FuaW1hbHMpXG5cbiMgVHJ5IHB1dHRpbmcgMiBvbiB0aGUgb3RoZXIgc2lkZSBvZiB0aGUgY29sb25cbm90aGVyWm9vQW5pbWFscyA9IGJpZ2dlclpvb1tfX19fXVxuXG4jIFByaW50IHRvIHNlZSB3aGF0IHlvdSdyZSBnZXR0aW5nIGJhY2tcbnByaW50KG90aGVyWm9vQW5pbWFscykiLCJzb2x1dGlvbiI6IiMgVXNlIHRoZSBzbGljZSBub3RhdGlvbiBsaWtlIHRoaXNcbnNvbWVab29BbmltYWxzID0gYmlnZ2VyWm9vWzI6IF1cblxuIyBQcmludCB0byBzZWUgd2hhdCB5b3UgZXhhY3RseSBzZWxlY3QgZnJvbSBgYmlnZ2VyWm9vYFxucHJpbnQoc29tZVpvb0FuaW1hbHMpXG5cbiMgVHJ5IHB1dHRpbmcgMiBvbiB0aGUgb3RoZXIgc2lkZSBvZiB0aGUgY29sb25cbm90aGVyWm9vQW5pbWFscyA9IGJpZ2dlclpvb1s6Ml1cblxuIyBQcmludCB0byBzZWUgd2hhdCB5b3UncmUgZ2V0dGluZyBiYWNrXG5wcmludChvdGhlclpvb0FuaW1hbHMpIiwic2N0IjoicHJlZGVmX21zZyA9IFwiRGlkIHlvdSBwdXQgcGFzcyBgOjJgIHRvIHRoZSBpbmRleCBvcGVyYXRvcj9cIlxuem9vQW5pbWFsc19wcmludF9tc2cgPSBcIkRpZCB5b3UgcHJpbnQgYG90aGVyWm9vQW5pbWFsc2A/XCJcbiMgVGVzdCBsaXN0IGBsaXN0XG50ZXN0X29iamVjdChcIm90aGVyWm9vQW5pbWFsc1wiLCB1bmRlZmluZWRfbXNnPXByZWRlZl9tc2csIGluY29ycmVjdF9tc2c9cHJlZGVmX21zZylcbiMgVGVzdCBwcmludFxudGVzdF9mdW5jdGlvbihcbiAgICBcInByaW50XCIsXG4gICAgbm90X2NhbGxlZF9tc2c9cHJlZGVmX21zZyxcbiAgICBpbmNvcnJlY3RfbXNnPXpvb0FuaW1hbHNfcHJpbnRfbXNnLFxuICAgIGRvX2V2YWw9RmFsc2VcbilcbnN1Y2Nlc3NfbXNnKFwiWW91IHJvY2shXCIpIn0=



You see that now we get the list, but only from the 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 and . Now we start from index 0 and go to index 2. Contrary to what you might have expected, the result does not include the entry.

In general, the following holds:

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:

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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiYmlnZ2VyWm9vID0gWydiZWFyJywgJ2xpb24nLCAncGFuZGEnLCAnemVicmEnLCBbJ2NoaW1wYW56ZWVzJywgJ2dvcmlsbGFzJywgJ29yYW5ndXRhbnMnLCAnZ2liYm9ucyddXSIsInNhbXBsZSI6IiMgUHJpbnQgdG8gc2VlIGhvdyB0aGUgc3RlcCB2YWx1ZSBpbmZsdWVuY2VzIHlvdXIgcmVzdWx0XG5wcmludChiaWdnZXJab29bMjo6Ml0pXG5wcmludChiaWdnZXJab29bMTo6M10pIn0=



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 package:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IGBjaG9pY2VgIGZyb20gdGhlIGByYW5kb21gIGxpYnJhcnlcbmZyb20gX19fXyBpbXBvcnQgX19fX1xuXG4jIENvbnN0cnVjdCB5b3VyIGBsaXN0YCB2YXJpYWJsZSB3aXRoIGEgbGlzdCBvZiB0aGUgZmlyc3QgNCBsZXR0ZXJzIG9mIHRoZSBhbHBoYWJldFxubGlzdCA9IFsnXycsJ18nLCdfJywnXyddXG5cbiMgUHJpbnQgeW91ciByYW5kb20gJ2xpc3QnIGVsZW1lbnRcbnByaW50KGNob2ljZShsaXN0KSkiLCJzb2x1dGlvbiI6IiMgSW1wb3J0IGBjaG9pY2VgIGZyb20gdGhlIGByYW5kb21gIGxpYnJhcnlcbmZyb20gcmFuZG9tIGltcG9ydCBjaG9pY2VcblxuIyBDb25zdHJ1Y3QgeW91ciBgbGlzdGAgdmFyaWFibGUgd2l0aCBhIGxpc3Qgb2YgdGhlIGZpcnN0IDQgbGV0dGVycyBvZiB0aGUgYWxwaGFiZXRcbmxpc3QgPSBbJ2EnLCAnYicsICdjJywgJ2QnXVxuXG4jIFByaW50IHlvdXIgcmFuZG9tICdsaXN0JyBlbGVtZW50XG5wcmludChjaG9pY2UobGlzdCkpIiwic2N0IjoicHJlZGVmX21zZyA9IFwiRGlkIHlvdSBhZGQgc29tZSBjb2RlIHRvIGNyZWF0ZSB0aGUgYGxpc3RgIHZhcmlhYmxlP1wiXG5pbXBvcnRfbXNnID0gXCJEb24ndCBmb3JnZXQgdG8gaW1wb3J0IHRoZSAncmFuZG9tJyBsaWJyYXJ5XCJcbnJhbmRvbV9saXN0X3ByaW50X21zZyA9IFwiRGlkIHlvdSBwcmludCB5b3UgcmFuZG9tIGBsaXN0YCBlbGVtZW50P1wiXG4jIFRlc3QgbGlzdCBgbGlzdFxudGVzdF9vYmplY3QoXCJsaXN0XCIsIHVuZGVmaW5lZF9tc2c9cHJlZGVmX21zZywgaW5jb3JyZWN0X21zZz1wcmVkZWZfbXNnKVxuIyBUZXN0IHByaW50XG50ZXN0X2Z1bmN0aW9uKFxuICAgIFwicHJpbnRcIixcbiAgICBub3RfY2FsbGVkX21zZz1wcmVkZWZfbXNnLFxuICAgIGluY29ycmVjdF9tc2c9cmFuZG9tX2xpc3RfcHJpbnRfbXNnLFxuICAgIGRvX2V2YWw9RmFsc2VcbilcbnN1Y2Nlc3NfbXNnKFwiQXdlc29tZSBqb2IhXCIpIn0=



If you also want to select a random list element through the indices of your list, you can work with method from the library:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IGByYW5kcmFuZ2VgIGZyb20gdGhlIGByYW5kb21gIGxpYnJhcnlcbmZyb20gX19fXyBpbXBvcnQgX19fX1xuXG4jIENvbnN0cnVjdCB5b3VyIGByYW5kb21MZXR0ZXJzYCB2YXJpYWJsZSB3aXRoIGEgbGlzdCBvZiB0aGUgZmlyc3QgNCBsZXR0ZXJzIG9mIHRoZSBhbHBoYWJldFxucmFuZG9tTGV0dGVycyA9IFsnXycsJ18nLCAnXycsICdfJ11cblxuIyBTZWxlY3QgYSByYW5kb20gaW5kZXggZnJvbSAncmFuZG9tTGV0dGVyc2BcbnJhbmRvbUluZGV4ID0gcmFuZHJhbmdlKDAsbGVuKHJhbmRvbUxldHRlcnMpKVxuXG4jIFByaW50IHlvdXIgcmFuZG9tIGVsZW1lbnQgZnJvbSBgcmFuZG9tYFxucHJpbnQocmFuZG9tTGV0dGVyc1tyYW5kb21Jbm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cbiAgICBkb19ldmFsPUZhbHNlXG4pXG5zdWNjZXNzX21zZyhcIldlbGwgZG9uZSFcIikifQ==



Tip: you should consider checking out the 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 . This operation allows you to glue together all strings in your list together and return them as a string. Read more here.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgTGlzdCBvZiBTdHJpbmdzIHRvIGEgU3RyaW5nXG5saXN0T2ZTdHJpbmdzID0gWydPbmUnLCAnVHdvJywgJ1RocmVlJ11cbnN0ck9mU3RyaW5ncyA9ICcnLmpvaW4obGlzdE9mU3RyaW5ncylcbnByaW50KHN0ck9mU3RyaW5ncylcblxuIyBMaXN0IE9mIEludGVnZXJzIHRvIGEgU3RyaW5nXG5saXN0T2ZOdW1iZXJzID0gWzEsIDIsIDNdXG5zdHJPZk51bWJlcnMgPSAnJy5qb2luKHN0cihuKSBmb3IgbiBpbiBsaXN0T2ZOdW1iZXJzKVxucHJpbnQoc3RyT2ZOdW1iZXJzKSJ9



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 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 function. Pass your list to this function and you will get a tuple back!

Remember: tuples are immutable. You can’t change them afterwards!

Tip: you can try transforming your 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 function. Just pass your list to it!

Now practice converting your list into a tuple and a set here:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoibGlzdE9mU3RyaW5ncyA9IFsnT25lJywgJ1R3bycsICdUaHJlZSddIiwic2FtcGxlIjoiIyBQYXNzIHlvdXIgbGlzdCB0byBgdHVwbGUoKWBcbnR1cGxlKF9fXylcblxuIyBUcmFuc2Zvcm0geW91ciBsaXN0IGludG8gYSBzZXRcbnNldChfX18pIiwic29sdXRpb24iOiIjIFBhc3MgeW91ciBsaXN0IHRvIGB0dXBsZSgpYFxudHVwbGUobGlzdE9mU3RyaW5ncylcblxuIyBUcmFuc2Zvcm0geW91ciBsaXN0IGludG8gYSBzZXRcbnNldChsaXN0T2ZTdHJpbmdzKSIsInNjdCI6InByZWRlZl9tc2cgPSBcIkRpZCB5b3UgcGFzcyBgbGlzdE9mU3RyaW5nc2AgdG8gYm90aCBgdHVwbGUoKWAgYW5kIGBzZXQoKWA/XCJcbiMgVGVzdCBsZW5ndGggYGp1c3RBTGlzdGBcbnRlc3RfZnVuY3Rpb24oXG4gICAgXCJ0dXBsZVwiLFxuICAgIG5vdF9jYWxsZWRfbXNnPXByZWRlZl9tc2csXG4gICAgaW5jb3JyZWN0X21zZz1wcmVkZWZfbXNnLFxuICAgIGRvX2V2YWw9RmFsc2VcbilcbnRlc3RfZnVuY3Rpb24oXG4gICAgXCJzZXRcIixcbiAgICBub3RfY2FsbGVkX21zZz1wcmVkZWZfbXNnLFxuICAgIGluY29ycmVjdF9tc2c9cHJlZGVmX21zZyxcbiAgICBkb19ldmFsPUZhbHNlXG4pXG5zdWNjZXNzX21zZyhcIkdvb2Qgam9iIVwiKSJ9



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:

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 .

actually works like expected: it zips elements together. In this case, when you zip the helloWorld elements and , your output will be:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiaGVsbG9Xb3JsZCA9IFsnaGVsbG8nLCd3b3JsZCcsJzEnLCcyJ10iLCJzYW1wbGUiOiJsaXN0KHppcChoZWxsb1dvcmxkKSkifQ==



Note that you need to use to print out the result of the function.

You will pass this to the function, which will interpret as a key and as a value. Similarly, will be interpreted as a key and as a value.

Run the code below to confirm this:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiaGVsbG9Xb3JsZCA9IFsnaGVsbG8nLCd3b3JsZCcsJzEnLCcyJ10iLCJzYW1wbGUiOiIjIENvbnZlcnQgdG8gYSBkaWN0aW9uYXJ5XG5oZWxsb1dvcmxkRGljdGlvbmFyeSA9IGRpY3QoemlwKGhlbGxvV29ybGRbMDo6Ml0sIGhlbGxvV29ybGRbMTo6Ml0pKVxuXG4jIFByaW50IG91dCB0aGUgcmVzdWx0XG5wcmludChoZWxsb1dvcmxkRGljdGlvbmFyeSkifQ==



Note that the second element that is passed to the function makes use of the step value to make sure that only the and elements are selected. Likewise, the first element uses the step value to select and .

If your list is large, you will probably want to do something like this:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6ImEgPSBbMSwgMiwgMywgNCwgNV1cblxuIyBDcmVhdGUgYSBsaXN0IGl0ZXJhdG9yIG9iamVjdFxuaSA9IGl0ZXIoYSlcblxuIyBaaXAgYW5kIGNyZWF0ZSBhIGRpY3Rpb25hcnlcbnByaW50KGRpY3QoemlwKGksIGkpKSkifQ==



Note that an iterable object can give you an iterator. The iterator, which has a method, holds information on where exactly you are in your iteration: it knows what is the next element in the iteration.

4. How To Determine The Size Of Your List in Python

You can pass your list to the function to get the length of your list back.

Test this out below! We have already made a list which you can use.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoianVzdEFMaXN0ID0gWzEsMiwzLCA0LCA1LCA2LCA3LCA4LDksIDEwXSIsInNhbXBsZSI6IiMgUGFzcyBganVzdEFMaXN0YCB0byBgbGVuKClgXG5sZW4oX19fXykiLCJzb2x1dGlvbiI6IiMgUGFzcyBganVzdEFMaXN0YCB0byBgbGVuKClgXG5sZW4oanVzdEFMaXN0KSIsInNjdCI6InByZWRlZl9tc2cgPSBcIkRpZCB5b3UgcGFzcyBganVzdEFMaXN0YCB0byB0aGUgYGxlbigpYCBmdW5jdGlvbj9cIlxuIyBUZXN0IGxlbmd0aCBganVzdEFMaXN0YFxudGVzdF9mdW5jdGlvbihcbiAgICBcImxlblwiLFxuICAgIG5vdF9jYWxsZWRfbXNnPXByZWRlZl9tc2csXG4gICAgaW5jb3JyZWN0X21zZz1wcmVkZWZfbXNnLFxuICAgIGRvX2V2YWw9RmFsc2VcbilcbnN1Y2Nlc3NfbXNnKFwiVGhhdCB3YXMgZWFzeSFcIikifQ==



Note that the function is not specifically to retrieve the lenght 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 and 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 method, like in this piece of code:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgVGhpcyBpcyB5b3VyIGxpc3Rcbmxpc3QgPSBbNSwgNiwgNywgOCwgOV1cblxuIyBDaGVjayB3aGV0aGVyIGl0J3MgaXRlcmFibGVcbmxpc3QuX19pdGVyX18ifQ==



Run the code above and try out some other simple examples in the IPython shell!

Now that the concept of an iterable is clear in our minds, we can begin to understand the difference between the two methods. , 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.

, on the other hand, adds its argument to the end of the list as a single item, which means that when the 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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoic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ByZWRlZl9tc2csXG4gICAgaW5jb3JyZWN0X21zZz1saXN0X3ByaW50X21zZyxcbiAgICBkb19ldmFsPUZhbHNlXG4pXG4jIFRlc3Q6IFByaW50IGxvbmdlckxpc3QgY29udGVudHNcbmxpc3RfcHJpbnRfbXNnMiA9IFwiRGlkIHlvdSBwdXQgYGxvbmdlckxpc3RgIGluIGBwcmludCgpYD9cIlxudGVzdF9mdW5jdGlvbihcbiAgICBcInByaW50XCIsXG4gICAgbm90X2NhbGxlZF9tc2c9cHJlZGVmX21zZyxcbiAgICBpbmNvcnJlY3RfbXNnPWxpc3RfcHJpbnRfbXNnMixcbiAgICBkb19ldmFsPUZhbHNlXG4pXG5zdWNjZXNzX21zZyhcImBhcHBlbmQoKWAgYW5kIGBleHRlbmQoKWAgaG9sZCBubyBzZWNyZXRzIGZvciB5b3UhXCIpIn0=



You can see that is a more efficient version of calling 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. is different from the operator, which in its turn is very similar to the method, but not exactly the same… You see that with and , 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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoic2hvcnRMaXN0ID0gWzEsIDIsIDNdIiwic2FtcGxlIjoiIyBDb25jYXRlbmF0ZSBgc2hvcnRMaXN0YCB3aXRoIGBbNCw1XWBcbnBsdXNMaXN0ID0gc2hvcnRMaXN0ICsgWzQsNV1cblxuI1VzZSB0aGUgYHByaW50KClgIG1ldGhvZCB0byBzZWUgYHBsdXNMaXN0YFxucHJpbnQoX19fXykiLCJzb2x1dGlvbiI6IiMgQ29uY2F0ZW5hdGUgYHNob3J0TGlzdGAgd2l0aCBgWzQsNV1gXG5wbHVzTGlzdCA9IHNob3J0TGlzdCArIFs0LDVdXG5cbiNVc2UgdGhlIGBwcmludCgpYCBtZXRob2QgdG8gc2VlIGBwbHVzTGlzdGBcbnByaW50KHBsdXNMaXN0KSIsInNjdCI6InByZWRlZl9tc2cgPSBcIkRpZCB5b3UgcGFzcyBgcGx1c0xpc3RgIHRvIHRoZSBgcHJpbnQoKWAgZnVuY3Rpb24/XCJcbnBsdXNfcHJpbnRfbXNnID0gXCJEaWQgeW91IHB1dCBgcGx1c0xpc3RgIGluIGBwcmludCgpYD9cIlxuIyBUZXN0IHByaW50IHBsdXNMaXN0XG50ZXN0X2Z1bmN0aW9uKFxuICAgIFwicHJpbnRcIixcbiAgICBub3RfY2FsbGVkX21zZz1wcmVkZWZfbXNnLFxuICAgIGluY29ycmVjdF9tc2c9cGx1c19wcmludF9tc2csXG4gICAgZG9fZXZhbD1GYWxzZVxuKVxuc3VjY2Vzc19tc2coXCJFYXN5IHBlYXN5IVwiKSJ9



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 method
  • Or you use the function and pass a list to it

The 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, , and , which you can sort for yourself using the two techniques described above :)

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoicm9vbXMgPSBbMzk3LCA4NywgMjk4LCA1MDMsIDQwMl1cbm9yZGVycyA9IFs3NiwgMywgNCwgNV0iLCJzYW1wbGUiOiIjIFVzZSBzb3J0KCkgb24gdGhlIHJvb21zIGxpc3Rcbl9fX18uc29ydCgpXG5cbiMgUHJpbnQgb3V0IHJvb21zIHRvIHNlZSB0aGUgcmVzdWx0XG5wcmludChfX19fKVxuXG4jIE5vdyB1c2UgdGhlIHNvcnRlZCgpIGZ1bmN0aW9uIG9uIG9yZGVyc1xuc29ydGVkKF9fX18pXG5cbiMgUHJpbnQgb3V0IG9yZGVyc1xucHJpbnQoX19fXykiLCJzb2x1dGlvbiI6IiMgVXNlIGBzb3J0KClgIG9uIHRoZSBgcm9vbXNgIGxpc3RcbnJvb21zLnNvcnQoKVxuXG4jIFByaW50IG91dCBgcm9vbXNgIHRvIHNlZSB0aGUgcmVzdWx0XG5wcmludChyb29tcylcblxuIyBOb3cgdXNlIHRoZSBgc29ydGVkKClgIGZ1bmN0aW9uIG9uIHRoZSBgb3JkZXJzYCBsaXN0XG5zb3J0ZWQob3JkZXJzKVxuXG4jIFByaW50IG91dCBvcmRlcnNcbnByaW50KG9yZGVycykiLCJzY3QiOiJwcmVkZWZfbXNnID0gXCJQdXQgaW4gYHJvb21zLnNvcnQoKWAsIHByaW50IGByb29tc2AgYW5kIHRoZW4gdXNlIGBzb3J0ZWQob3JkZXJzKWAgdG8gc29ydCB0aGUgb3JkZXJzIGFnYWluLlwiXG4jIFRlc3Qgc29ydCByb29tcyB3aXRoIHNvcnQoKVxudGVzdF9mdW5jdGlvbihcInJvb21zLnNvcnRcIiwgbm90X2NhbGxlZF9tc2c9XCJNYWtlIHN1cmUgdG8gc29ydCB0aGUgYHJvb21zYCBsaXN0LlwiKVxuIyBUZXN0OiBQcmludCBmaWxlIGNvbnRlbnRzXG5wcmludF9yb29tc19tc2cgPSBcIkRpZCB5b3UgcHV0IGByb29tc2AgaW4gYHByaW50KClgP1wiXG50ZXN0X2Z1bmN0aW9uKFxuICAgIFwicHJpbnRcIixcbiAgICBub3RfY2FsbGVkX21zZz1wcmVkZWZfbXNnLFxuICAgIGluY29ycmVjdF9tc2c9cHJpbnRfcm9vbXNfbXNnLFxuICAgIGRvX2V2YWw9RmFsc2VcbilcbnRlc3RfZnVuY3Rpb24oXCJzb3J0ZWRcIiwgbm90X2NhbGxlZF9tc2c9XCJNYWtlIHN1cmUgdG8gcGFzcyB0aGUgYG9yZGVyc2AgbGlzdCB0byBgc29ydGVkKClgLlwiKVxuc3VjY2Vzc19tc2coXCJHcmVhdCBqb2IhXCIpIn0=



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:
  • You can use the built-in function:
  • You can use the library:
    • With the method:
    • If your list contains objects and you want to copy those as well, you can use :

Try it out below! We have already created a list for you to copy and share with your friends.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiZ3JvY2VyaWVzID0gWydhcHBsZXMnLCAnZWdncycsICdzdGVhaycsICdzcGFnaGV0dGknLCBbJ2ZhbnRhJywgJ3Nwcml0ZScsICdjb2xhJ11dIiwic2FtcGxlIjoiIyBDb3B5IHRoZSBncm9jZXJ5IGxpc3QgYnkgc2xpY2luZyBhbmQgc3RvcmUgaXQgaW4gdGhlIGBuZXdHcm9jZXJpZXNgIHZhcmlhYmxlXG5uZXdHcm9jZXJpZXMgPSBncm9jZXJpZXNbX19dXG5cbiMgQ29weSB0aGUgZ3JvY2VyeSBsaXN0IHdpdGggdGhlIGBsaXN0KClgIGZ1bmN0aW9uIGFuZCBzdG9yZSBpdCBpbiBhIGBncm9jZXJpZXNGb3JGcmllbmRzYCB2YXJpYWJsZVxuZ3JvY2VyaWVzRm9yRnJpZW5kcyA9IGxpc3QoX19fXylcblxuIyBJbXBvcnQgdGhlIGNvcHkgbGlicmFyeSBhcyBjXG5pbXBvcnQgX19fXyBhcyBfX1xuXG4jIENyZWF0ZSBhIGBncm9jZXJpZXNGb3JGYW1pbHlgIHZhcmlhYmxlIGFuZCBhc3NpZ24gdGhlIGNvcGllZCBncm9jZXJ5IGxpc3QgdG8gaXRcbmdyb2Nlcmllc0ZvckZhbWlseSA9ICBjLmNvcHkoX19fXylcblxuIyBVc2UgYGRlZXBjb3B5KClgIGFuZCBhc3NpZ24gdGhlIGNvcGllZCBsaXN0IHRvIGEgYGdyb2Nlcmllc0ZvcktpZHNgIHZhcmlhYmxlXG5ncm9jZXJpZXNGb3JLaWRzPSBjLmRlZXBjb3B5KF9fX18pIiwic29sdXRpb24iOiIjIENvcHkgdGhlIGdyb2NlcnkgbGlzdCBieSBzbGljaW5n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AgIG5vdF9jYWxsZWRfbXNnPXByZWRlZl9tc2csXG4gICAgaW5jb3JyZWN0X21zZz1jb3B5X2RlZXBjb3B5X21zZyxcbiAgICBkb19ldmFsPUZhbHNlXG4pXG5cbnN1Y2Nlc3NfbXNnKFwiVGhhdCB3YXMgYSBsb3Qgb2Ygd29yayEgQXdlc29tZSA6KVwiKSJ9



Note that when you use the ‘simple’ copy methods, your original lists will be modified. However, if you use the method, this will be prevented. Run the code below to see what happens:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgVGhpcyBpcyB5b3VyIGxpc3Rcbm9iamVjdExpc3QgPSBbJ2EnLCdiJyxbJ2FiJywnYmEnXV1cblxuIyBDb3B5IHRoZSBgb2JqZWN0TGlzdGBcbmNvcGllZExpc3QgPSBvYmplY3RMaXN0WzpdXG5cbiMgQ2hhbmdlIHRoZSBmaXJzdCBsaXN0IGVsZW1lbnQgb2YgYGNvcGllZExpc3RgXG5jb3BpZWRMaXN0WzBdID0gJ2MnXG5cbiMgR28gdG8gdGhlIHRoaXJkIGVsZW1lbnQgKHRoZSBuZXN0ZWQgbGlzdCkgYW5kIGNoYW5nZSB0aGUgc2Vjb25kIGVsZW1lbnRcbmNvcGllZExpc3RbMl1bMV0gPSAnZCdcblxuIyBQcmludCBvdXQgdGhlIG9yaWdpbmFsIGxpc3QgdG8gc2VlIHdoYXQgaGFwcGVuZWQgdG8gaXRcbnByaW50KG9iamVjdExpc3QpIn0=



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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6Ilt4KioyIGZvciB4IGluIHJhbmdlKDEwKV0ifQ==



Now, what does this mean? You take all numbers from 0 to 10, where 10 is not included, and you multiple 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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6Ilt4KioyIGZvciB4IGluIHJhbmdlKDEwKSBpZiB4JTI9PTBdIn0=



You just add a condition, indicated by , 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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6Im15TGlzdCA9IFsxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5LCAxMF1cblsobGFtYmRhIHg6IHgqeCkoeCkgZm9yIHggaW4gbXlMaXN0XSJ9



In this example, you already have a list that you’re transforming through list comprehension: you see that again , for every list element in , some operation will be done.

Next, you see that 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 .

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 .

In other words, you could rewrite the whole second line of the above chunk of code as:

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 method:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgQ291bnQgdGhlIG9jY3VycmVuY2VzIG9mIHRoZSBudW1iZXIgNFxucHJpbnQoWzEsIDIsIDksIDQsIDUsIDQsIDFdLmNvdW50KDQpKVxuXG4jIENvdW50IHRoZSBvY2N1cnJlbmNlcyBvZiB0aGUgbGV0dGVyIFwiYVwiXG5saXN0ID0gW1wiZFwiLCBcImFcIiwgXCJ0XCIsIFwiYVwiLCBcImNcIiwgXCJhXCIsIFwibVwiLCBcInBcIl1cbmxpc3QuY291bnQoXCJhXCIpIn0=



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

To count the occurrences of items in a list, you can also use list comprehension in combination with the method:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6Imxpc3Q9IFtcImFcIixcImJcIixcImJcIl1cbltbeCxsaXN0LmNvdW50KHgpXSBmb3IgeCBpbiBzZXQobGlzdCldIn0=



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 and . Then you say that for each set item, you want to take that item and pass it to a method

Remember that sets only contain unique items!

In this case, first will be passed to and all occurrences of in your original list will be counted.

Counting all list items with

Alternatively, there’s the faster method from the library:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IGBDb3VudGVyYCBmcm9tIHRoZSBgY29sbGVjdGlvbnNgIGxpYnJhcnlcbmZyb20gY29sbGVjdGlvbnMgaW1wb3J0IENvdW50ZXJcblxuIyBUaGlzIGlzIHlvdXIgbGlzdFxubGlzdCA9IFtcImFcIixcImJcIixcImJcIl1cblxuIyBQYXNzIGBsaXN0YCB0byBgQ291bnRlcigpYFxuQ291bnRlcihsaXN0KSJ9



Note that 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 function in combination with :

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgWW91ciBsaXN0IGB4YFxueCA9IFsxLDIsMyw0LDUsNiw3LDgsOV1cblxuIyBTcGxpdCBgeGAgdXAgaW4gY2h1bmtzIG9mIDNcbnkgPSB6aXAoKltpdGVyKHgpXSozKVxuXG4jIFVzZSBgbGlzdCgpYCB0byBwcmludCB0aGUgcmVzdWx0IG9mIGB6aXAoKWBcbmxpc3QoeSkifQ==



The code above works as follows:

  • is an iterator over a sequence.
  • produces a list with three objects: each list iterator is an iterator of .
  • the that is passed to the function before anything else unpacks the sequence into arguments so that you’re passing the same iterator three times to the 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:

  1. You will have three list iterator objects, which you can think of as:

  1. The first time, will take one element of the list sequentially, which leaves you with:

Note that the iterator objects will keep track for you which element is next in the iteration!

  1. The second time, elements will be added to the three lists you just created, and you will end up with:

  1. The last time, you follow the same procedure, and you end up with:

  1. Zipping these three lists together will leave you with:

If, however, you decide to put this logic into a function, you can follow this approach:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgTWV0aG9kIHRvIHNwbGl0IHVwIHlvdXIgbGlzdHMgaW50byBjaHVua3NcbmRlZiBjaHVua3MobGlzdCwgY2h1bmtTaXplKTpcbiAgICBcIlwiXCJZaWVsZCBzdWNjZXNzaXZlIGNodW5rU2l6ZS1zaXplZCBjaHVua3MgZnJvbSBsaXN0LlwiXCJcIlxuICAgIGZvciBpIGluIHJhbmdlKDAsIGxlbihsaXN0KSwgY2h1bmtTaXplKTpcbiAgICAgICAgeWllbGQgbGlzdFtpOmkgKyBjaHVua1NpemVdXG5cbiMgVXNlIHlvdXIgYGNodW5rc2AgZnVuY3Rpb24gdG8gcHJpbnQgb3V0IGNodW5rcyBvZiB0aGUgc2FtZSBzaXplXG5pbXBvcnQgcHByaW50XG5wcHJpbnQucHByaW50KGxpc3QoY2h1bmtzKHJhbmdlKDEwLCA3NSksIDEwKSkpIn0=



Your 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 method).

Then, you see : 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 value you pass to .

For each element in this list constructed by , 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 or pretty print to print your result.

You can also you can simply use list comprehension instead of writing a function:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgU2V0IHVwIHlvdXIgbGlzdCBhbmQgY2h1bmsgc2l6ZVxubGlzdCA9IHJhbmdlKDAsIDUwKVxuY2h1bmsgPSA1XG5cbiMgU3BsaXQgdXAgeW91ciBsaXN0IGludG8gY2h1bmtzXG5bbGlzdFtpOmkgKyBjaHVua10gZm9yIGkgaW4gcmFuZ2UoMCwgbGVuKGxpc3QpLCBjaHVuayldIn0=



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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgVGhpcyBpcyB5b3VyIGxpc3Rcbm15bGlzdCA9IFtbMSwyLDNdLFs0LDUsNiw3XSxbOCw5LDEwXV1cblxuIyBMb29wIG92ZXIgeW91ciBsaXN0IGFuZCBwcmludCBhbGwgZWxlbWVudHMgdGhhdCBhcmUgb2Ygc2l6ZSAzXG5mb3IgeCBpbiBteWxpc3Q6XG4gICAgICBpZiBsZW4oeCk9PTM6XG4gICAgICAgIHByaW50KHgpIn0=



You have a list 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, and , only these will be printed out.

Note how in the code chunk above can be changed to , 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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgVGhpcyBpcyB5b3VyIGxpc3Rcbm15TGlzdCA9IFszLDQsNSw2XVxuXG4jIExvb3Agb3ZlciBgbXlMaXN0YCBhbmQgcHJpbnQgdHVwbGVzIG9mIGFsbCBpbmRpY2VzIGFuZCB2YWx1ZXMgXG5mb3IgaSwgdmFsIGluIGVudW1lcmF0ZShteUxpc3QpOlxuICAgICBwcmludChpLCB2YWwpIn0=



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 , which will return an index and a value. Naturally, we have to show this also in the beginning of our for statement: you see 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 :

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoibXlMaXN0ID0gW1sxLDIsM10sWzQsNSw2LDddLFs4LDksMTBdXSIsInNhbXBsZSI6Ilt4IGZvciB4IGluIG15TGlzdCBpZiBsZW4oeCk9PTNdIn0=



13. How To Create Flat Lists Out Of Lists

To make a simple list out of a list of lists, you can use the function. Just don’t forget to pass your list and an empty list to it:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgWW91ciBpbml0aWFsIGxpc3Qgb2YgbGlzdHNcbmxpc3QgPSBbWzEsMl0sWzMsNF0sWzUsNl1dXG5cbiMgRmxhdHRlbiBvdXQgeW91ciBvcmlnaW5hbCBsaXN0IG9mIGxpc3RzIHdpdGggYHN1bSgpYFxuc3VtKGxpc3QsIFtdKSJ9



You can also use a two different approaches to flatten your lists:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoibGlzdE9mTGlzdHMgPSBbWzEsMl0sWzMsNF0sWzUsNl1dIiwic2FtcGxlIjoiIyBZb3UgY2FuIHJlZHVjZSB0aGUgbGlzdHMgb2YgbGlzdHMgb2YgbGlzdHMgbGlrZSB0aGlzXG5mcm9tIGZ1bmN0b29scyBpbXBvcnQgcmVkdWNlXG5wcmludChyZWR1Y2UobGFtYmRhIHgseTogeCt5LGxpc3RPZkxpc3RzKSlcblxuIyBPciB5b3UgY2FuIHVzZSBsaXN0IGNvbXByZWhlbnNpb25cbnByaW50KFtpdGVtIGZvciBzdWJsaXN0IGluIGxpc3RPZkxpc3RzIGZvciBpdGVtIGluIHN1Ymxpc3RdKSJ9



  • You either use the method, to which you pass a lambda function. Now, the 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 is added to and this result is added to . 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 or , , and then you want to consider , printing out each item from the sublist.

Another way of seeing this nested list comprehension is to rewrite it as a regular for loop:

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 function.

This is illustrated in the exercise below. We have already defined two lists for you, and . Use the IPython shell if you want to check the contents of the lists.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoibGlzdDEgPSBbMSwgNiwgNywgMTAsIDEzLCAyOCwgMzIsIDQxLCA1OCwgNjNdXG5saXN0MiA9IFtbMTMsIDE3LCAxOCwgMjEsIDMyXSwgWzcsIDExLCAxMywgMTQsIDI4XSwgWzEsIDUsIDYsIDgsIDE1LCAxNl1dIiwic2FtcGxlIjoiIyBJbnRlcnNlY3QgYm90aCBsaXN0cyB3aXRoIGxpc3QgY29tcHJlaGVuc2lvblxuaW50ZXJzZWN0aW9uID0gW2xpc3QoZmlsdGVyKGxhbWJkYSB4OiB4IGluIGxpc3QxLCBzdWJsaXN0KSkgZm9yIHN1Ymxpc3QgaW4gbGlzdDJdXG5cbiMgUHJpbnQgdGhlIHJlc3VsdCBvZiB0aGUgaW50ZXJzZWN0aW9uXG5wcmludChfX19fKSIsInNvbHV0aW9uIjoiIyBJbnRlcnNlY3QgYm90aCBsaXN0cyB3aXRoIGxpc3QgY29tcHJlaGVuc2lvblxuaW50ZXJzZWN0aW9uID0gW2xpc3QoZmlsdGVyKGxhbWJkYSB4OiB4IGluIGxpc3QxLCBzdWJsaXN0KSkgZm9yIHN1Ymxpc3QgaW4gbGlzdDJdXG5cbiMgUHJpbnQgdGhlIHJlc3VsdCBvZiB0aGUgaW50ZXJzZWN0aW9uXG5wcmludChpbnRlcnNlY3Rpb24pIiwic2N0IjoicHJlZGVmX21zZyA9IFwiZGlkIHlvdSBwYXNzIGBpbnRlcnNlY3Rpb25gIHRvIHRoZSBgcHJpbnQoKWAgZnVuY3Rpb24/XCJcbiMgVGVzdCBgcHJpbnRgXG50ZXN0X2Z1bmN0aW9uKFxuICAgIFwicHJpbnRcIixcbiAgICBub3RfY2FsbGVkX21zZz1wcmVkZWZfbXNnLFxuICAgIGluY29ycmVjdF9tc2c9cHJlZGVmX21zZyxcbiAgICBkb19ldmFsPUZhbHNlXG4pXG5zdWNjZXNzX21zZyA9IFwiV2F5IHRvIGdvIVwiIn0=



In the piece of code above, the part takes each sublist’s item and checks to see if it is in the source list . The list comprehension is executed for each sublist in .

Note that, since returns an iterable instead of a list, you need to wrap all you put into your with . Try removing the to test this out for yourself!

For better readability, you could also work with nested list comprehensions:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoibGlzdDEgPSBbMSwgNiwgNywgMTAsIDEzLCAyOCwgMzIsIDQxLCA1OCwgNjNdXG5saXN0MiA9IFtbMTMsIDE3LCAxOCwgMjEsIDMyXSwgWzcsIDExLCAxMywgMTQsIDI4XSwgWzEsIDUsIDYsIDgsIDE1LCAxNl1dIiwic2FtcGxlIjoiIyBBbiBpbnRlcnNlY3Rpb24gb2YgYm90aCBsaXN0cywgc3RvcmVkIGluIGBpbnRlcnNlY3Rpb25gXG5pbnRlcnNlY3Rpb24gPSBbW3ggZm9yIHggaW4gc3VibGlzdCBpZiB4IGluIGxpc3QxXSBmb3Igc3VibGlzdCBpbiBsaXN0Ml1cblxuIyBQcmludCB0aGUgcmVzdWx0IG9mIHRoZSBpbnRlcnNlY3Rpb25cbnByaW50KGludGVyc2VjdGlvbikifQ==



Intersecting Lists With

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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoibGlzdDEgPSBbMSwgNiwgNywgMTAsIDEzLCAyOCwgMzIsIDQxLCA1OCwgNjNdXG5saXN0MiA9IFsxMywgMTcsIDE4LCAyMSwgMzIsIDcsIDExLCAxMywgMTQsIDI4LCAxLCA1LCA2LCA4LCAxNSwgMTZdIiwic2FtcGxlIjoiIyBNYWtlIHVzZSBvZiB0aGUgbGlzdCBhbmQgc2V0IGRhdGEgc3RydWN0dXJlc1xucHJpbnQobGlzdChzZXQobGlzdDEpICYgc2V0KGxpc3QyKSkpXG5cbiMgVXNlIGBpbnRlcnNlY3Rpb24oKWBcbnByaW50KGxpc3Qoc2V0KGxpc3QxKS5pbnRlcnNlY3Rpb24obGlzdDIpKSkifQ==



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 .

Note that for these two approaches to work, your list should not contain other lists. That is why 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 function. If you later need a real list again, you can similarly pass the set to the function.

Remember that an iterable can be a list, dictionary, … Check whether a variable is an iterable by applying the method to it.

The following example clearly demonstrates the use of to delete all duplicates from your list:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgWW91ciBsaXN0IHdpdGggZHVwbGljYXRlIHZhbHVlc1xuZHVwbGljYXRlcyA9IFsxLCAyLCAzLCAxLCAyLCA1LCA2LCA3LCA4XVxuXG4jIFByaW50IHRoZSB1bmlxdWUgYGR1cGxpY2F0ZXNgIGxpc3RcbnByaW50KGxpc3Qoc2V0KGR1cGxpY2F0ZXMpKSlcblxuIyBBIGxpc3Qgd2l0aCBzbWFsbCBudW1iZXJzIFxuc21hbGxOdW1iZXJzID0gWzEsIDIsIDNdXG5cbiMgUHJpbnQgdGhlIHVuaXF1ZSBgZHVwbGljYXRlc2AgbGlzdCB3aXRob3V0IHRoZSBzbWFsbCBudW1iZXJzXG5saXN0KHNldChkdXBsaWNhdGVzKSAtIHNldChzbWFsbE51bWJlcnMpKSJ9



As you can see from the last result, the original order of your list is not maintained after you detract the set elements of . 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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6ImltcG9ydCBudW1weVxubnVtcHkuYXJyYXkoW10pIn0=



However, if your goal is to initialize a NumPy array, you can also follow one of the following approaches:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiaW1wb3J0IG51bXB5Iiwic2FtcGxlIjoiIyBNYWtlIGEgTnVtUHkgYXJyYXkgb2YgZm91ciByb3dzIGFuZCB0d28gY29sdW1ucyBhbmQgZmlsbGVkIHdpdGggMFxubnVtcHkuemVyb3Moc2hhcGU9KDQsMikpXG5cbiMgTWFrZSBhIE51bVB5IGFycmF5IG9mIDEgdmFsdWVzIG9mIHRocmVlIGNvbHVtbnNcbm51bXB5Lm9uZXMoMylcblxuIyBNYWtlIGFuIGVtcHR5IE51bVB5IGFycmF5XG5udW1weS5lbXB0eShzaGFwZT0oMCwwKSkifQ==



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:

You can easily calculate the weighted average with the following pieces of code:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiY29zdCA9IFswLjQyNCwgMC40MjIxLCAwLjQxODUsIDAuNDEzMiwgMC40MTNdXG5jYXNlcyA9IFsxMCwgMjAsIDMwLCA0MCwgNTBdIiwic2FtcGxlIjoiZm9yIGMgaW4gcmFuZ2UobGVuKGNvc3QpKTpcbiAgIGNvc3RbY10gPSAoY29zdFtjXSAqIGNhc2VzW2NdIC8gc3VtKGNhc2VzKSlcbmNvc3QgPSBzdW0oY29zdClcbnByaW50KGNvc3QpIn0=



But, you can also go for one of these two other approaches. Firstly, you can use:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiY29zdCA9IFswLjQyNCwgMC40MjIxLCAwLjQxODUsIDAuNDEzMiwgMC40MTNdXG5jYXNlcyA9IFsxMCwgMjAsIDMwLCA0MCwgNTBdIiwic2FtcGxlIjoic3VtKGNvc3RbY10gKiBjYXNlc1tjXSAvIHN1bShjYXNlcykgZm9yIGMgaW4gcmFuZ2UobGVuKGNvc3QpKSkifQ==



Or you can use this one:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiY29zdCA9IFswLjQyNCwgMC40MjIxLCAwLjQxODUsIDAuNDEzMiwgMC40MTNdXG5jYXNlcyA9IFsxMCwgMjAsIDMwLCA0MCwgNTBdIiwic2FtcGxlIjoic3VtKGNvc3RbY10gKiBjYXNlc1tjXSBmb3IgYyBpbiByYW5nZShsZW4oY29zdCkpKSAvIHN1bShjYXNlcykifQ==



Lastly, you can also you calculate the weighted average of your list with the help of the function.

Tip: make a variable to which you just assign the function, with the and 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.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiY29zdCA9IFswLjQyNCwgMC40MjIxLCAwLjQxODUsIDAuNDEzMiwgMC40MTNdXG5jYXNlcyA9IFsxMCwgMjAsIDMwLCA0MCwgNTBdIiwic2FtcGxlIjoiIyBTZWUgd2hhdCBgemlwKClgIGRvZXMgdG8geW91ciBgY29zdGAgYW5kIGBjYXNlc2BcbnByaW50KGxpc3QoemlwKF9fX18sIF9fX18pKSlcblxuIyBDYWxjdWxhdGUgdGhlIHdlaWdodGVkIGF2ZXJhZ2VcbnByaW50KHN1bShbeCAqIHkgZm9yIHgsIHkgaW4gemlwKGNvc3QsIGNhc2VzKV0pIC8gc3VtKGNhc2VzKSkiLCJzb2x1dGlvbiI6IiMgU2VlIHdoYXQgYHppcCgpYCBkb2VzIHRvIHlvdXIgbGlzdHNcbnByaW50KGxpc3QoemlwKGNvc3QsIGNhc2VzKSkpXG5cbiMgQ2FsY3VsYXRlIHRoZSB3ZWlnaHRlZCBhdmVyYWdlXG5wcmludChzdW0oW3ggKiB5IGZvciB4LCB5IGluIHppcChjb3N0LCBjYXNlcyldKSAvIHN1bShjYXNlcykpIiwic2N0IjoicHJlZGVmX21zZyA9IFwiRG9uJ3QgZm9yZ2V0IHRvIGFkZCB0aGUgYHppcCgpYCBmdW5jdGlvbiBhbmQgdG8gcGFzcyBgY29zdGAgYW5kIGBjYXNlYCB0byBpdCFcIlxuIyBUZXN0IG9iamVjdCBjb3N0QW5kQ2FzZXNcbnRlc3RfZnVuY3Rpb24oXCJ6aXBcIiwgXG4gICAgICAgICAgICAgIG5vdF9jYWxsZWRfbXNnID0gXCJIYXZlIHlvdSBjcmVhdGVkIHRoZSBjb3N0QW5kQ2FzZXMgdmFyaWFibGU/IERvbid0IGZvcmdldCB0byBwYXNzIGBjb3N0YCBhbmQgYGNhc2VzYCB0byBgemlwKClgIHRvIGNyZWF0ZSBpdCFcIixcbiAgICAgICAgICAgICAgaW5jb3JyZWN0X21zZz1wcmVkZWZfbXNnLCBcbiAgICAgICAgICAgICAgZG9fZXZhbD1GYWxzZSlcbnN1Y2Nlc3NfbXNnKFwiQXdlc29tZSB3b3JrIVwiKSJ9



Remember what does to your lists? Easy, it zips your lists together, literally. For example, would output .

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:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgSW1wb3J0IG51bXB5IGFzIG5wXG5pbXBvcnQgX19fXyBhcyBfX1xuXG4jIE1ha2UgYSBOdW1QeSBhcnJheVxuYSA9IG5wLmFycmF5KFsxLDIsMyw0LDVdKVxuXG4jIFJldHVybiB0aGUgNTB0aCBwZXJjZW50aWxlIG9mIG91ciBOdW1QeSBhcnJheVxucCA9IG5wLnBlcmNlbnRpbGUoYSwgNTApXG5cbiNQcmludCB0aGUgcmVzdWx0XG5wcmludChfX19fKSIsInNvbHV0aW9uIjoiIyBJbXBvcnQgbnVtcHkgYXMgbnBcbmltcG9ydCBudW1weSBhcyBucFxuXG4jIE1ha2UgYSBOdW1QeSBhcnJheVxuYSA9IG5wLmFycmF5KFsxLDIsMyw0LDVdKVxuXG4jIFJldHVybiB0aGUgNTB0aCBwZXJjZW50aWxlIG9mIG91ciBOdW1QeSBhcnJheVxucCA9IG5wLnBlcmNlbnRpbGUoYSwgNTApXG5cbiNQcmludCB0aGUgcmVzdWx0XG5wcmludChwKSIsInNjdCI6InByZWRlZl9tc2cgPSBcIkhhdmUgeW91IGltcG9ydGVkIG51bXB5IGFuZCBwcmludGVkIG91dCB0aGUgcGVyY2VudGlsZSByZXN1bHQ/XCJcbnF1YW50aWxlX3ByaW50X21zZyA9IFwiRG9uJ3QgZm9yZ2V0IHRvIHBhc3MgYHBgIHRvIGBwcmludCgpYCFcIlxuaW1wb3J0X21zZyA9IFwiSGF2ZSB5b3UgaW1wb3J0ZWQgbnVtcHk/XCJcbnRlc3RfZnVuY3Rpb24oXG4gICAgXCJwcmludFwiLFxuICAgIG5vdF9jYWxsZWRfbXNnPXByZWRlZl9tc2csXG4gICAgaW5jb3JyZWN0X21zZz1xdWFudGlsZV9wcmludF9tc2csXG4gICAgZG9fZXZhbD1GYWxzZVxuKVxuIyBUZXN0IGltcG9ydCBgbnVtcHlgXG50ZXN0X2ltcG9ydChcIm51bXB5XCIsIHNhbWVfYXMgPSBUcnVlLCBub3RfaW1wb3J0ZWRfbXNnID0gaW1wb3J0X21zZywgaW5jb3JyZWN0X2FzX21zZyA9IHByZWRlZl9tc2cpXG5zdWNjZXNzX21zZyhcIkZhbnRhc3RpYyFcIikifQ==



How To Sum Lists Element-Wise

Let’s say you have two lists:

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 to perform an element-wise addition:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoibGlzdDE9WzEsIDIsIDNdXG5saXN0Mj1bNCwgNSwgNl0iLCJzYW1wbGUiOiJmcm9tIG9wZXJhdG9yIGltcG9ydCBhZGRcbmxpc3QobWFwKGFkZCwgbGlzdDEsIGxpc3QyKSkifQ==



This will result in a list .

Note how you will need to make use of to print the result of the function.

Or you can use the function with a list comprehension:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoibGlzdDE9WzEsIDIsIDNdXG5saXN0Mj1bNCwgNSwgNl0iLCJzYW1wbGUiOiJbc3VtKHgpIGZvciB4IGluIHppcChsaXN0MSwgbGlzdDIpXSJ9



… With NumPy

The previous examples worked perfectly on these small lists, but when you’re working with slightly more data, you should probably use to make the process easier.

You should then make sure that you import the library and that your lists are converted into NumPy arrays:

Comments

Leave a Reply

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