In this Python tutorial, we will discuss how to create a list in Python. Also, we will discuss, how to create a list of floats in Python. We will see various examples of how to make a list of floats in Python.
Create a list in Python
A list in Python is an ordered, mutable collection of items, where each item can be of any data type such as strings, numbers, or other lists. Lists are a versatile and essential data structure in Python that allows you to store and manipulate multiple items together.
A list in Python is a mutable, ordered collection of items, enclosed in square brackets ([]). Each item can be of any data type, such as strings, numbers, or even other lists.
To create a list in Python, follow these steps:
Step 1: Initialize an empty list
cities = []
Step 2: Add items to the list
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
You can also create a list using the list()
constructor:
cities = list(("New York", "Los Angeles", "Chicago", "Houston", "Phoenix"))
Create list of floats in Python
Let us see, how to create a list of floats in Python.
A list of floats is a list containing floating-point numbers. To create a list of floats, follow the same steps as for creating a list, but use floating-point numbers as items.
Step 1: Initialize an empty list of floats
city_populations = []
Step 2: Add float items to the list
city_populations = [8.4, 3.9, 2.7, 2.3, 1.7]
Here, each number represents the population (in millions) of the respective city in the ‘cities’ list.
Create a list of floats in Python using for loop
You can also create a list of floats using a for loop, which is particularly useful when you want to generate a sequence of float numbers.
Example:
float_list = [float(i) / 10 for i in range(1, 11)]
print(float_list)
Output:
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
Create a list of floats in Python using map() Function
Another approach to create a list of floats is by using the map() function. This function applies a given function to each item of an iterable (e.g., list, tuple) and returns a new iterable (e.g., a list or a map object).
Example:
int_list = [1, 2, 3, 4, 5]
float_list = list(map(float, int_list))
print(float_list)
Output:
[1.0, 2.0, 3.0, 4.0, 5.0]
How to make a list of floats in python using numpy Library
Let us see, how to make a list of floats in python using numpy Library.
The numpy library is a powerful tool for working with numerical data in Python. You can use it to generate an array of float numbers, which can be easily converted to a list.
Example:
import numpy as np
float_array = np.linspace(1.0, 5.0, num=5)
float_list = float_array.tolist()
print(float_list)
Output:
[1.0, 2.0, 3.0, 4.0, 5.0]
Python list of floats
Here’s another example of how to create a list of float numbers in Python. In this example, we will use a list comprehension to generate a list of float numbers representing square roots of the first ten positive integers.
Example:
import math
# Using a list comprehension to create a list of float numbers (square roots)
square_roots = [math.sqrt(i) for i in range(1, 11)]
print(square_roots)
Output:
[1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903, 3.0, 3.1622776601683795]
When you run the code, you can see the output:
Here we learned, how to create a list in Python, and how to create a list of floats in Python with various examples.
You may also like:
- Python list methods with examples
- Convert a List of Characters into a String in Python
- Convert NumPy Array to List of Lists in Python
- How to convert numpy array to list of strings in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.