goglbalance.blogg.se

List comprehension python
List comprehension python






  1. List comprehension python how to#
  2. List comprehension python update#
  3. List comprehension python code#
  4. List comprehension python zip#

Try not to use the x list or the y list in any of the following code: do everything with the data list.Īlso, include at least one list comprehesion in your program. Start your program with the statementsĪnd then do everything else in the rest of the program using the data list. We should go in and set all of those cross rates to be exactly 1. The diagonal entries represent exchanges where the origin currency and the destination currency are the same. Here is one final thing we should do with the resulting table. This is a nested set of list comprehensions, where the outer comprehension uses an inner comprehension to make lists of cross rates, producing a list of lists as the final result.Ĭrosses = for o,r1 in exchange] Here is a list comprehension that makes a table of all possible cross rates. For example, if we wanted to set up an exchange from GBP to CNY, we would first convert 1 GBP to 1/0.760425 dollars. We can use our list of US dollar exchange rates to make cross rates for any pair of currencies in the exchange list. A currency cross rate is an exchange rate from one currency to another, with an origin currency and a destination currency. For example, if we wanted to make a list of powers of 2 we could use the following code: We could do this by using a combination of a loop and the list append() method.Ī list comprehension provides a more compact way to accomplish the same thing.Īnother common application of list comprehensions is making lists from ranges. Suppose we wanted to make a list of just the currency codes from the example above. List comprehensions are frequently used to extract data from other lists or to generate new data from an existing data list.

List comprehension python zip#

Here is the table printing example rewritten to use a zip list:Ī Python list comprehension is a method for constructing a list from a list, a range, or other iterable structure. A zip list formed from a pair of lists is a single lists of tuples, with each pair of data items from the original lists becoming one tuple in the zip list.

List comprehension python code#

This code iterates over a list of index values and uses those index values to access elements of both lists.Īn alternative approach is to construct a zip list from the original pair of lists and just iterate over that. In an earlier lecture I showed some code to print a list of data points in a table. A common situation where you might want to do this is printing a table. Sometimes you will want to combine data from two separate lists into a single list. Here some code that will do this.Īmount = float(input('How many units do you want to buy? ')Īn alternative to using the index notation for tuples in a for loop is to use a form of the for loop that assigns each member of a tuple to a different loop variable:

list comprehension python

The program will compute and print how many US dollars it would take to purchase that many units of the currency. Suppose we wanted to write a program that prompts the user to enter a code for a foreign currency and an amount of that currency that the user wants to purchase. ('NZD', 1.54148), ('MXN', 18.99626), ('SGD', 1.3809)]īecause the list spans multiple lines, I have used the Python continuation character, \, at the end of each line. The numbers in each case show how many units of the currency one US dollar would buy.Įxchange = [('EUR', 0.869335), ('JPY', 113.0005) ,\ The list below shows currency codes for a number of currencies and the exchange rate from US dollars to that currency.

list comprehension python

Tuples are especially useful in situations where data items belong together and would not make sense by themselves.

List comprehension python update#

You can also update the entries in a tuple by assigning new values to them. You can access the elements in a tuple using the same index notation you would use with a list. The tuple in the example above has two items in it: this size is fixed and can never be changed. Unlike lists, which can have items appended or removed from them via the use of list methods, the tuple is an immutable structure.

list comprehension python list comprehension python

List comprehension python how to#

The example below shows how to construct a tuple. The Python tuple is an alternative to the list structure.








List comprehension python