<jupyter_start><jupyter_text>Lists-1 Lists are exactly the same as arrays. We need lists to store multiple data elements in one place. You may ask that the same thing is done by tuples - yes, but the problem with tuples is that they're immutable i.e. their values cannot be modified etc. This is where lists come in. A list is nothing but a collection of elements of various kinds.The list type is a container that holds a number of other objects, in a given order. The list type implements the sequence protocol, and also allows you to add and remove objects from the sequence.Lists are simply declared in square brackets (not round brackets, or that'll be a tuple declaration). Note that a list need not have all elements of the same datatype; we can store integers, boolean values, strings etc. in one array itself.<jupyter_code># Declaring a list

myList = [1,'hello!',3.4563]

myList1 = list()<jupyter_output><empty_output><jupyter_text>How do we access elements of a list? It is done very easily using the same method of zero-indexing as in strings. For instance, if a = [1,2,3] is a list, then a[2] will return 3.<jupyter_code># Accessing list elements

print(myList[1])
print(myList[-1])
print(myList[0:2])<jupyter_output>hello!
3.4563
[1, 'hello!']<jupyter_text>The next thing that comes is taking input from the user and storing it into a list. We can do that by taking in elements one by one, but what if we input all the elements at once (separated by spaces)? This is where we need to use a few functions for help.We'll use the strip() and split() functions to help us take input. When we take a list as input in the usual way, we get elements all at once with spaces in between. If we want them one by one (without including the spaces), we first strip() the spaces in the beginning and end so they aren't counted as elements, then split() the list on spaces, and finally store it in another list.<jupyter_code># Taking input in a list

myStr = input().strip()
myList = myStr.split(" ")
print(myList)

# The above can be done in a single line.

myList = [int(x) for x in input().strip().split(" ")]
print(myList)<jupyter_output>1 2 3 4
['1', '2', '3', '4']
1 2 3 4 5
[1, 2, 3, 4, 5]<jupyter_text>Lists-2 We next look at some further characteristics and functionalities of lists. We can add elements to our list in three ways: by using the list.append(element) function, the list.insert(index,element) function and the list.extend(another_list) function. The advantage of using insert() is that we can insert the elements in exactly our position of choice. The extend function appends all the elements of a list to another.<jupyter_code># Adding elements to list
myList = [1,2,'world']
emptyList = list()

myList.append(3)

myList.insert(1,'hello')

emptyList.extend(myList)
print(emptyList)<jupyter_output>[1, 'hello', 2, 'world', 3]<jupyter_text>We can delete elements from a list in three ways: by using the list.pop() function, the list.remove() function and del. If we don't specify an index in pop() then the last element will be deleted, while specifying an index will delete the element at that index in the list. The remove() function takes as input the element value you want to delete, not the index. Del can be used to remove multiple elements using slicing.<jupyter_code># Deleting elements from list

myList.pop();

myList.append(4);
myList.append(5);

myList.pop(0);

myList.remove(4);

del myList[0:1]

print(myList)<jupyter_output>[2, 'world', 5]<jupyter_text>Note that we cannot add strings, integers etc. to the list by arithmetic operators. Only lists can be added to other lists using the '+' operator.There are some other functions we can perform on lists. The sort() function sorts the list in increasing order, while the count() function will return the number of elements in the list.<jupyter_code>#myList = myList + 2    # Wrong!
myList = myList + [1,2] # Correct

newList = [3,2,1,4]
newList.sort()
print(newList)<jupyter_output>[1, 2, 3, 4]<jupyter_text>Bubble Sort Bubble sort is a popular and basic sorting algorithm that pushes the largest element to the end in every iteration. It compares each element to the adjacent one, and swaps if the adjacent one is smaller than the element. Given below is a simple code for Bubble Sort.<jupyter_code>def bubbleSort(nlist):
    for passnum in range(len(nlist)-1,0,-1):
        for i in range(passnum):
            if nlist[i]>nlist[i+1]:
                temp = nlist[i]
                nlist[i] = nlist[i+1]
                nlist[i+1] = temp

nlist = [14,46,43,27,57,41,45,21,70]
bubbleSort(nlist)
print(nlist)<jupyter_output>[14, 21, 27, 41, 43, 45, 46, 57, 70]<jupyter_text>Dictionaries Dictionaries are same as hash maps in other languages. They are a collection of key-value pairs: that is, values are mapped to keys. The keys can be of any nature - integer, strings, tuples etc. (But lists cannot be used as keys.)Like lists are represented with square brackets, dictionaries are represented by curly brackets. The keys of a dictionary are immutable, and cannot be changed; the values, however, can be changed. If we set a key with a certain value, it will update the value in the dictionary if the key exists, or it will create a new key-value pair if the key does not already exist.Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.<jupyter_code># Initializing a dictionary

myDict = {}

myDict[2]=6
myDict["hello"]=4

print(myDict)<jupyter_output>{2: 6, 'hello': 4}<jupyter_text>Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.Some operators on dictionaries are as follows:            Operators        Explanation                len(d)        returns the number of stored entries, i.e. the number of (key,value) pairs.                del d[k]        deletes the key k together with his value                k in d        True, if a key k exists in the dictionary d        k not in d    True, if a key k doesn't exist in the dictionary d    We can insert values into a dictionary by simply writing a new key-value pair, delete by using del, and iterate using the usual iteration methods. We can also query dictionaries, access their keys and values etc.<jupyter_code># iterating over a dictionary
for i in myDict:
    print(i)

# deleting elements from a dictionary
del myDict['hello']
print(myDict)<jupyter_output>2
hello
{2: 6}
{'fraction_non_alphanumeric': 0.07752521451151588, 'fraction_numerical': 0.01791359325605901, 'mean_word_length': 4.162393162393163, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '373138', 'license': 'Apache-2.0', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1878, 'n_words': 982, 'path': 'notes/3 lists and dictionary/Lists and Dictionaries.ipynb', 'repo_name': 'subhendu17620/ML-Coding-Ninjas', 'size': '6643'}