Python Data Structures ( Lists, Tuples, Dictionaries )

 


S.NoData TypesSyntaxExampleDescriptionNature
1List
have brackets []
list_name = [element1, element2, element3, element4, and so on]
movies = [Inception, Avengers, Intersteller, Dun Kirk, 1920, Pikachu, The Joker]
print(movies[1] (this will print Avengers movie, because the index starts from 0 , 1, 2, 3 and so on. SO for the Joker movie the index will be 6. 
this will be printed as =  print(movies[6])

Also : -
print(movies[1:3] = Avengers, Interstellar ( never going to print 3 index, it will stop before three if you have mentioned 1:3)
print(movies[1:] = all elements
print(movies[:1] =  Only element which is present on index 0.
print(movies[-1]) = grab the last element of the list
print(len(movies)) - how many number of elements available.
movies.append = "Home Alone"  = appended in the list
movies.pop() = automatically deletes last element of the list
movies.pop(0) = delete elements with the help of index.
Mutable (can be appended, can insert other element, can iterate.)
2Tuples
have brackets ()
tuple_name = ("element1", "element2", "element3", "element4", "element5", "and so on..")
grade = ("a", "b", "c", "d", "e")
print(grades[1])
same as list but tuples are non mutable/fixed
Non-Mutable (cannot be appended, can not insert other element, can not iterate.)
3Dictionaries
Key:value pairs
dictionary_name={"key:value", "key:value", "key:value", "key:value, and so on..."}

Phone_quatity = {"iPhone: 9", "Andoird:10", "Blacberry: 3", "Windows: 7"}
employees = {"Finance":["bob", "tom", "joe", "teedy", "john", "rock"],  "IT" : ["matt", "harry", "tim", "tintin", "happy", "nick"], "HR": ["mark", "saw", "mick", "peter", "dom", "ethan","ricky"]}

A dictionary can easly hold multiple lists in it. In above example too we have mentioned lists in the place of value filed in each element of the dictionary.

To append the dictionary with other key:value pair = employees['legal'] = ["Mr. X"].

To append the dictionary with other key:value pair = employees.update({sales:["Andy", "Oliver"]})

To update the dictionary with other key:value pair, suppose you had a dictionary phone with element iphone:9 ( iphone is a key and 9 is a value) = phone['iPhone'] = 4 (this will update the value (quantity), from 9 to 4 )

Also if we want to check the value in particular key then = print(phone.get("iPhone"))  (it will show the value of iPhone key, which is 4 as per above scenario.)






Mutable (can be appended, can insert other element, can iterate.)

Comments