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:
- 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)
- 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)
- 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)
- 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)
- 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:
- List of numbers:
numbers = [1, 2, 3, 4, 5]
Code language: Python (python)
- List of strings:
fruits = ["apple", "banana", "orange", "grape"]
Code language: Python (python)
- List of mixed data types:
mixed_list = [1, "hello", 3.14, True]
Code language: Python (python)
- Nested lists (list of lists):
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Code language: Python (python)
- List of boolean values:
flags = [True, False, True, True, False]
Code language: Python (python)
- List of characters:
characters = ['a', 'b', 'c', 'd']
Code language: Python (python)
- List of lists (2D list):
data = [['Alice', 25], ['Bob', 30], ['Carol', 22]]
Code language: Python (python)
- 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;
- How to write update query in MySQL in Python?
- Simple Example Python Programs for Practice [Beginners ]
- What is for loop in python with example [10 Examples]
- Multiprocessing Python Example For Loop
- What is class and object in Python with example?
- What is an example of a user-defined function in Python?
- Can R and Python be used together? [With Example]
- How does format () work in Python?
- What is .2f Python format?
- What is the e function in Python?
- How do you write an automation test script in Python?
- How do you automate daily tasks in Python?