How to Create List in Python | List Complete Tutorial for Beginners

Publicado el: 27 julio 2023
en el canal de: Pycent com
23
1

Notes prepared by Pycent.com
Python Lists
A list in Python is a data structure that holds an ordered collection of items, i.e., you can store a sequence of items in a list. These items can be of any type, and a list can contain items of different types.
Code Examples

empty_list = [ ] # An empty list
numbers_list = [1, 2, 3, 4, 5] # A list of integers
fruits_list = ["Apple", "Banana", "Cherry"] # A list of strings
mixed_list = [1, "Good Day !", 3.16, True] # A list with mixed data types
nested_list = [1, "Hi", [2, "Work till you can see yourself there"], True] # A list with another list as an item

Code Example
How to Access Elements from List
fruits = ["Apple", "Banana", "Cherry"]
print(fruits[0]) # Outputs: Apple
print(fruits[1]) # Outputs: Banana
print(fruits[-1]) # Outputs: Cherry, negative indexing starts from the end

How to perform List Slicing
Code Example
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(numbers[2:5]) # Outputs: [3, 4, 5], includes the start index and excludes the end index
print(numbers[:5]) # Outputs: [1, 2, 3, 4, 5], if start index is not provided, it starts from the beginning
print(numbers[5:]) # Outputs: [6, 7, 8, 9, 10], if end index is not provided, it goes up to the end


How to Append Elements in List-Adding Elements to List

Code Example
fruits = ["Apple", "Banana", "Cherry"]
fruits.append("Dragonfruit") # Add a single item to the end
print(fruits) # Outputs: ['Apple', 'Banana', 'Cherry', 'Dragonfruit']

more_fruits = ["Elderberry", "Fig", "Grape"]
fruits.extend(more_fruits) # Add multiple items to the end
print(fruits) # Outputs: ['Apple', 'Banana', 'Cherry', 'Dragonfruit', 'Elderberry', 'Fig', 'Grape']

fruits.insert(0, "Avocado") # Insert 'Avocado' at position 0
print(fruits) # Outputs: ['Avocado', 'Apple', 'Banana', 'Cherry', 'Dragonfruit', 'Elderberry', 'Fig', 'Grape']

How to Remove Elements from List
Code Example
fruits = ["Apple", "Banana", "Cherry"]
fruits.remove("Banana") # Remove a specific item
print(fruits) # Outputs: ['Apple', 'Cherry']

removed_fruit = fruits.pop(0) # Remove the item at position 0 and get its value
print(removed_fruit) # Outputs: 'Apple'
print(fruits) # Outputs: ['Cherry']


Queries
Python
Programming
Data Structures
Lists in Python
Python Lists
List Operations
Python Programming
Python Tutorial
Learning Python
Python for Beginners
Python Data Types
List Manipulation
Python List Methods
Python List Operations
Python Coding
Coding Tutorial
Computer Science
Software Development
Coding in Python
Python Basics
Learn to Code


En esta página del sitio puede ver el video en línea How to Create List in Python | List Complete Tutorial for Beginners de Duración hora minuto segunda en buena calidad , que subió el usuario Pycent com 27 julio 2023, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 23 veces y le gustó 1 a los espectadores. Disfruta viendo!