# List & tuple & set & dictionary in Python

- Link: https://technotaught.com/list-tuple-set-dictionary-in-python/
- Published: 2020-03-14T15:06:11+05:30
- Author: Ashishkumar Vishwakarma

In this, we study what is the difference between List & tuple & set & dictionary
in Python.

 * The **list** is a collection that is ordered and changeable. Allows duplicate
   members.
 * A **tuple **is a collection that is ordered and unchangeable. Allows duplicate
   members.
 * **Set **is a collection that is unordered and unindexed. No duplicate members.
 * **Dictionary **is a collection that is unordered, changeable and indexed. No 
   duplicate members.

## List & tuple & set & dictionary in Python

![List & tuple & set & dictionary in Python](https://technotaught.com/wp-content/
uploads/2020/03/List-tuple-set-dictionary-in-Python.jpg)

In Python, the array starts with 0.

## 1. List

 * The **list** is a collection that is ordered and changeable. Allows duplicate
   members.
 * In Python list is written with the Square bracket.
 * In List support both string and number.
 * Add and remove the elements is possible.

Example1:

    ```wp-block-code
    l= ["zen" , "yen", "ken"]
    print(l)
    ```

    ```wp-block-code
    Output
    >>['zen', 'yen', 'ken']
    ```

Example 2:

    ```wp-block-code
    l= ["zen", 30 , "yen", 40 ]
    print(l)
    print(l[2])
    print(l[2][0])
    print(l[2][-1])
    ```

    ```wp-block-code
    Output
    ['zen', 30, 'yen', 40]yen
    y
    n
    ```

### List Methods

Python has a set of built-in methods that you can use on lists.

| Method  |  Description  | 
| append() | Adds an element at the end of the list | 
| clear() | Removes all the elements from the list | 
|  copy()  | Returns a copy of the list | 
| count() | Returns the number of elements with the specified value | 
| extend() | Add the elements of a list (or any iterable), to the end of the current list | 
| index() | Returns the index of the first element with the specified value | 
| insert() | Adds an element at the specified position | 
| pop() | Removes the element at the specified position | 
| remove() | Removes the item with the specified value | 
| reverse() | Reverses the order of the list | 
| sort() | Sorts the list |

## 2. Tuple

 * A **tuple **is a collection that is **ordered** and **unchangeable**. Allows 
   duplicate members.
 *  In Python, **tuples** are written with round brackets.
 * **We cannot add and remove the elements**

Example :

    ```wp-block-code
    l= ("zen", 30 , "yen", 40 )
    print(l)
    ```

    ```wp-block-code
    Output
    ('zen', 30, 'yen', 40)
    ```

### Tuple Methods

Python has two built-in methods that you can use on tuples.

| Method  |  Description  | 
| index() | Searches the tuple for a specified value and returns the position of where it was found | 
| count() |  Returns the number of times a specified value occurs in a tuple  |

## 3. Set

 * **Set **is a collection that is unordered and unindexed.
 * No duplicate members.
 * In Python, **Set** are written with curly brackets.

Example:

    ```wp-block-code
    l= {"zen", 30 , "yen", 40 }
    print(l)
    ```

    ```wp-block-code
    Output
    {40, 'yen', 'zen', 30}
    ```

Set Methods

Python has a set of built-in methods that you can use on sets.

| Methods |  Description  | 
| add() | Adds an element to the set | 
| clear() | Removes all the elements from the set | 
| copy() | `Returns a copy of the set` | 
| difference() | Returns a set containing the difference between two or more sets | 
| difference_update() | Removes the items in this set that are also included in another, specified set | 
| discard() | Removes the items in this set that are also included in another, specified set | 
| intersection() | Returns a set containing the difference between two or more sets | 
| intersection_update() | Removes the items in this set that are not present in other, specified set(s) | 
| isdisjoint() | Returns whether two sets have an intersection or not | 
| issubset() | Returns whether another set contains this set or not | 
| issuperset()  | () Returns whether this set contains another set or not | 
| pop()  | Removes an element from the set | 
| remove() | Removes the specified element | 
| symmetric_difference()  | Returns a set with the symmetric differences of two sets | 
| symmetric_difference_update() | inserts the symmetric differences from this set and another | 
| union() | Return a set containing the union of sets | 
| update() | Update the set with the union of this set and others |

## 4. Dictionary

 * **Dictionary **is a collection that is unordered, changeable and indexed.
 * No duplicate members.
 * In Python, dictionaries are written with **curly** **brackets**, and they have**
   keys** and **values**.

Example:

    ```wp-block-code
    l= {"zen":30, "ben":40 , "yen":50, "ten":60 }
    print(l)
    ```

    ```wp-block-code
    Output
    {'zen': 30, 'ben': 40, 'yen': 50, 'ten': 60}
    ```

### Dictionary Methods

Python has a set of built-in methods that you can use on dictionaries.

|  Methods  |  Description  | 
| clear() | Removes all the elements from the dictionary | 
| copy() | Returns a copy of the dictionary | 
| fromkeys() | Returns a dictionary with the specified keys and value | 
| get() | Returns the value of the specified key | 
| items() | Returns a list containing a tuple for each key-value pair | 
| keys() | Returns a list containing the dictionary’s keys | 
| pop() | Removes the element with the specified key | 
| popitem() | Removes the last inserted key-value pair | 
| setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value | 
| update() | Updates the dictionary with the specified key-value pairs | 
| values() | Returns a list of all the values in the dictionary |

Source:- [w3schools](https://www.w3schools.com/)

Also View:- [Python Function-lambda |filter, map, reduce.](https://technotaught.com/python-function-lambda-filter-map-reduce/)

Also View:[ – Write a program to create a registration form using AWT](https://technotaught.com/registration-form/).

### Share this:

 * [  X ](https://technotaught.com/list-tuple-set-dictionary-in-python/?share=twitter)
 * [  Facebook ](https://technotaught.com/list-tuple-set-dictionary-in-python/?share=facebook)
 * [More](https://technotaught.com/list-tuple-set-dictionary-in-python/#)

 * [  WhatsApp ](https://technotaught.com/list-tuple-set-dictionary-in-python/?share=jetpack-whatsapp)
 * [  LinkedIn ](https://technotaught.com/list-tuple-set-dictionary-in-python/?share=linkedin)
 * [  Email ](https://technotaught.com/list-tuple-set-dictionary-in-python/?subject=%5BShared%20Post%5D%20List%20%26%20tuple%20%26%20set%20%26%20dictionary%20in%20Python&body=https%3A%2F%2Ftechnotaught.com%2Flist-tuple-set-dictionary-in-python%2F&share=email)
 * [  Reddit ](https://technotaught.com/list-tuple-set-dictionary-in-python/?share=reddit)
 * [  Tumblr ](https://technotaught.com/list-tuple-set-dictionary-in-python/?share=tumblr)
 * [  Pinterest ](https://technotaught.com/list-tuple-set-dictionary-in-python/?share=pinterest)
 * [  Telegram ](https://technotaught.com/list-tuple-set-dictionary-in-python/?share=telegram)
 * [  Print ](https://technotaught.com/list-tuple-set-dictionary-in-python/#print?share=print)

### _Related_

## Comments

### By: Steph B

- Link: https://technotaught.com/list-tuple-set-dictionary-in-python/#comment-142
- Published: 2020-04-25T15:16:15+05:30

Thanks for Posting. Keep up the great effort.\|
