Python List Tutorial | What is List, Advantages & 10 Examples

Python List Tutorial

A list in Python is a collection of items which is ordered, changeable (mutable), and allows duplicate values. Lists are very useful for storing multiple values in a single variable.

Python में list एक collection होती है जो ordered (क्रमबद्ध), changeable (परिवर्तनीय), और duplicate values को allow करती है। Lists का उपयोग एक ही variable में कई values रखने के लिए किया जाता है।

Where to use Lists?

  • When you want to store multiple related items.
  • When you want to perform operations on collections like adding, removing, or sorting elements.
  • When order of items matters.
  • जब आपको एक साथ कई related items store करने हों।
  • जब collection पर add, remove, या sort जैसे operations करने हों।
  • जब items का क्रम important हो।

Advantages of Lists

  • Dynamic size — can grow or shrink as needed.
  • Supports heterogeneous data types in a single list.
  • Easy to iterate through using loops.
  • Supports built-in functions like append(), remove(), sort(), etc.
  • Dynamic size — जरूरत के अनुसार बढ़ या घट सकती है।
  • एक list में अलग-अलग data types हो सकते हैं।
  • Loops से आसानी से access और process कर सकते हैं।
  • append(), remove(), sort() जैसे built-in functions सपोर्ट करती है।
Example 1: Create and Print a List
fruits = ['apple', 'banana', 'cherry']
print(fruits)

Create a list of fruits and print it.

फलों की list बनाएं और प्रिंट करें।

Sample Output:

['apple', 'banana', 'cherry']
Example 2: Access List Elements by Index
fruits = ['apple', 'banana', 'cherry']
print(fruits[1])

Access second element using index 1 (index starts at 0).

index 1 से दूसरा item access करें (index 0 से शुरू होता है)।

Sample Output:

banana
Example 3: Modify List Element
fruits = ['apple', 'banana', 'cherry']
fruits[1] = 'orange'
print(fruits)

Change second element from 'banana' to 'orange'.

दूसरे item को 'banana' से 'orange' में बदलें।

Sample Output:

['apple', 'orange', 'cherry']
Example 4: Add Element to List
fruits = ['apple', 'banana']
fruits.append('cherry')
print(fruits)

Use append() to add an element at the end.

append() से अंत में एक नया item जोड़ें।

Sample Output:

['apple', 'banana', 'cherry']
Example 5: Remove Element from List
fruits = ['apple', 'banana', 'cherry']
fruits.remove('banana')
print(fruits)

Remove 'banana' from the list using remove().

remove() से 'banana' को list से हटाएं।

Sample Output:

['apple', 'cherry']
Example 6: List Length
fruits = ['apple', 'banana', 'cherry']
print(len(fruits))

Get number of elements using len() function.

len() से list के elements की संख्या प्राप्त करें।

Sample Output:

3
Example 7: Iterate Through List
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)

Print each element using a for loop.

for loop से सभी items को प्रिंट करें।

Sample Output:

apple
banana
cherry
Example 8: Sort a List
numbers = [3, 1, 4, 2]
numbers.sort()
print(numbers)

Sort numbers in ascending order using sort().

sort() से numbers को ascending order में करें।

Sample Output:

[1, 2, 3, 4]
Example 9: Create List from User Input
n = int(input('Enter number of elements: '))
my_list = []
for i in range(n):
    val = input(f'Enter element {i+1}: ')
    my_list.append(val)
print('Your list:', my_list)

Create a list by reading elements from user input.

User input से list बनाएं।

Sample Output:

Enter number of elements: 3
Enter element 1: dog
Enter element 2: cat
Enter element 3: bird
Your list: ['dog', 'cat', 'bird']
Example 10: List with Different Data Types
my_list = [10, 'hello', 3.14, True]
print(my_list)

List can hold elements of different data types.

List में अलग-अलग data types के elements हो सकते हैं।

Sample Output:

[10, 'hello', 3.14, True]