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

Опубликовано: 27 Июль 2023
на канале: 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


На этой странице сайта вы можете посмотреть видео онлайн How to Create List in Python | List Complete Tutorial for Beginners длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Pycent com 27 Июль 2023, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 23 раз и оно понравилось 1 зрителям. Приятного просмотра!