What is a list () in Python With Example

In Python, list() is a built-in function used to create a new list. The list() function can take an iterable (like a string, tuple, dictionary, etc.) as an argument and convert it into a new list.

Here are some examples of using the list() function:

  1. Converting a string to a list of characters:
text = "Hello, World!"
char_list = list(text)
print(char_list)  # Output: ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']Code language: Python (python)
  1. Converting a tuple to a list:
tuple_data = (1, 2, 3, 4, 5)
list_data = list(tuple_data)
print(list_data)  # Output: [1, 2, 3, 4, 5]Code language: Python (python)
  1. Converting a range object to a list:
range_obj = range(1, 6)
list_range = list(range_obj)
print(list_range)  # Output: [1, 2, 3, 4, 5]Code language: Python (python)
  1. Converting a dictionary’s keys or values to a list:
data_dict = {'a': 1, 'b': 2, 'c': 3}
keys_list = list(data_dict.keys())
values_list = list(data_dict.values())
print(keys_list)  # Output: ['a', 'b', 'c']
print(values_list)  # Output: [1, 2, 3]Code language: Python (python)
  1. Creating an empty list:
empty_list = list()
print(empty_list)  # Output: []Code language: Python (python)

The list() function is useful for converting other iterable data types into lists, which allows you to take advantage of the list-specific functionalities like indexing, appending, and more.

What are the examples of list in Python?

In Python, lists are used to store collections of items. They are versatile and can contain elements of different data types. Here are some examples of lists in Python:

  1. List of numbers:
numbers = [1, 2, 3, 4, 5]Code language: Python (python)
  1. List of strings:
fruits = ["apple", "banana", "orange", "grape"]Code language: Python (python)
  1. List of mixed data types:
mixed_list = [1, "hello", 3.14, True]Code language: Python (python)
  1. Nested lists (list of lists):
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Code language: Python (python)
  1. List of boolean values:
flags = [True, False, True, True, False]Code language: Python (python)
  1. List of characters:
characters = ['a', 'b', 'c', 'd']Code language: Python (python)
  1. List of lists (2D list):
data = [['Alice', 25], ['Bob', 30], ['Carol', 22]]Code language: Python (python)
  1. Empty list:
empty_list = []Code language: Python (python)

Lists are mutable, meaning you can modify, add, or remove elements after creation.

To access individual elements within a list, you can use indexing, like list_name[index].

For example, fruits[0] would give you “apple” from the fruits list.

Lists also support various methods for operations like adding elements (e.g., append(), extend(), insert()), removing elements (e.g., remove(), pop()), and sorting (e.g., sort()).

Read More;

  • Yaryna Ostapchuk

    I am an enthusiastic learner and aspiring Python developer with expertise in Django and Flask. I pursued my education at Ivan Franko Lviv University, specializing in the Faculty of Physics. My skills encompass Python programming, backend development, and working with databases. I am well-versed in various computer software, including Ubuntu, Linux, MaximDL, LabView, C/C++, and Python, among others.

Leave a Comment