Skip to main content

Lists and Dictionary | Python Programming By Akash

 1.   Lists:

Definition: It is a built-in data structure in Python that allows us to store multiple items inside a single variable. Instead of creating variable1, variable2, variable3, we can put all of them into one List. It is like a shopping list or a backpack where you can keep many things—numbers, strings, or even other lists—all together in a specific order.

The Syntax: We use Square Brackets [ ] to create a list, and we separate items with a comma ,.

For Example,

1.1.  List Indexing:

Definition: It is the technique used to grab a specific single item from a List using its position number. Just like every house in a street has a number, every item in a list has a unique number called an Index.

Two Ways to Index:

a.   Positive Indexing (Left to Right):

§ Starts at 0 for the first item.

§ Goes up by 1 (0, 1, 2, 3...).

b.   Negative Indexing (Right to Left):

§ Python allows you to count backwards!

§ -1 is always the last item.

§ -2 is the second to last, and so on.

For Example,

1.2.  Append Method (.append()):

Definition: It is a built-in function used to add a single item to the end of a list. It acts like sticking a new note at the bottom of your to-do list. It modifies the original list directly.

The Syntax: list_name.append(new_item)

For Example,

1.3.  Insert Method (.insert()):

Definition: It is a built-in function that allows you to add a new item at a specific position inside the list. Unlike append (which only adds to the end), insert lets you choose exactly where the new item goes. It forces the existing items to move one step to the right to make space.

The Syntax: list_name.insert(index, new_item)

It needs two arguments:

                 i.    Index: Where do you want it? (The position number).

               ii.    Item: What do you want to add?

For Example,

1.4.  Extend Method (.extend()):

Definition: It is a built-in function used to merge a whole list of items into the current list. While append adds the list as a single blob (nesting it), extend unpacks the new list and adds every individual item to the end of the current list.

The Syntax: list_name.extend(another_list)

For Example,

1.5.  Remove Method (.remove()):

Definition: It is a built-in function used to delete the first matching item it finds in the list. You do not tell it the position number (index); instead, you tell it the actual value name (like 'Banana') that you want to get rid of.

The Syntax: list_name.remove(item_value)

For Example,

1.6.  Pop Method (.pop()):

Definition: It is a built-in function used to remove an item at a specific position (index). Unlike remove() (which deletes the item and throws it away), pop() removes the item and gives it back to you so you can save it in a variable.

The Syntax: list_name.pop(index)

For Example,

1.7.  Index Method (.index()):

Definition: It is a built-in function that does the opposite of accessing a list. Normally, we give an index to get a value (list[0]). This method allows us to give a value to find out its index position. It tells you where in the line a specific item is standing.

The Syntax: position = list_name.index(item_value)

For Example,

1.8.  Count Method (.count()):

Definition: It is a built-in function used to find out exactly how many times a specific item appears in your list. It scans the whole list from start to finish and returns a number (integer).

The Syntax: number = list_name.count(item_value)

For Example,

Comparison:

§  len(list): Counts everything in the list (Total size).

§  list.count(x): Counts only the specific item 'x'.

1.9.  Sort Method (.sort()):

Definition: It is a built-in function that arranges all the items in your list in a specific order (usually smallest to biggest, or A to Z). It helps keep your data organized.

The Syntax: list_name.sort()

Reverse Sort: If you want to sort from Biggest to Smallest (Z to A), you put a special command inside the parentheses: list_name.sort(reverse=True)

For Example,

1.10.   Reverse Method (.reverse()):

Definition: It is a built-in function that physically flips the order of the list items. The first item becomes the last, and the last item becomes the first. Unlike .sort(), it does not care about the value of the items (it doesn't check if they are big or small); it just reverses their current positions like a mirror image.

The Syntax: list_name.reverse()

For Example,

Comparison Summary:

§  list.sort(reverse=True): Organizes items from Big to Small (Logic involved).

§  list.reverse(): Just flips the list upside down (No logic involved).

  • Overview Image: 

2.   Dictionary:

Definition: It is a built-in data structure in Python that stores data in Key-Value pairs. Unlike a List (which uses numbers/indices to find items), a Dictionary uses unique 'Keys' (names/labels) to find values. It works exactly like a real-world dictionary: you look up a Word (Key) to find its Definition (Value).

The Syntax: We use Curly Braces {} to create a dictionary.

           ®    Structure: { Key : Value }

For Example,

1.1.  Accessing Items

Definition: To get a specific piece of information from a dictionary, you use square brackets [], just like a list. However, instead of putting a number (index) inside, you put the Key Name (the label).

The Syntax: variable = dictionary_name["Key_Name"]

For Example,



Safe Method (.get()): If you are not sure if a key exists, use .get(). It won't crash; it just returns None if the key is missing.

For Example,

1.2.  Add & Update Items:

Definition: In Python dictionaries, adding a new item and changing an old item use the exact same syntax. Python is smart enough to check the Key:

§ If the Key exists: It Updates (overwrites) the old value.

§ If the Key is new: It Adds the new Key-Value pair to the dictionary.

The Syntax: dictionary_name["Key"] = New_Value

For Example,

1.3.  Remove & Pop Items:

Definition: Just like lists, you can remove items from a dictionary. You can choose to remove a specific item by its Key or remove the last item added.

The Three Ways to Delete:

a. .pop("Key"):

§  Removes the item with that specific Key.

§  Returns the value (so you can save it).

§  Safe: You can provide a default value to avoid crashing if the key is missing: .pop("Key", "Not Found").

b. .popitem():

§  Removes the Last Inserted item (the most recent one you added).

§  Returns the whole pair (Key, Value) as a tuple.

§  Note: In very old Python versions (before 3.7), this was random. Now it is strictly "Last In, First Out".

c. del dictionary["Key"]:

§  Deletes the item immediately. It does not return the value.

§  Warning: If the Key doesn't exist, it crashes.

For Example,

1.4.  View Methods (Keys, Values, & Items Methods):

Definition: Sometimes you don't want the whole dictionary. You might just want a list of all the Labels (Keys), or just a list of all the Data (Values). These three methods allow you to break a dictionary apart into its components.

The Syntax:

a.   dictionary.keys() => Returns a list of all Keys.

b.   dictionary.values() => Returns a list of all Values.

c.    dictionary.items() => Returns a list of Pairs (tuples).

For Example,

  • Overview Image:



Comments