-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_in_python.py
35 lines (31 loc) · 956 Bytes
/
list_in_python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
marks = [98, 99, 100, "Maths"]
print(marks)
print(marks[1]) # in list print by index number . .
print(marks[-1: -3]) # also indexing in negative . .
print(marks[0: 2])
# using for loop in list . .
for score in marks:
print(score)
# nesting dictionary in list . .
travel_log = [
{
"country": "India",
"cities_visited": ["Mumbai", "Delhi", "pune"],
"total_visits": 10
},
{
"country": "korea",
"cities_visited": ["seoul", "daegu", "busan"],
"total_visits": 5
}
]
# function for dictionary elements. . .
def add_new_country(country, cities_visited, total_visits):
new_country = {}
new_country["country"] = country
new_country["cities_visited"] = cities_visited
new_country["total_visits"] = total_visits
travel_log.append(new_country)
# calling function . .
add_new_country("USA", ["New York", "Chicago", "Los Angeles", "Boston"], 10)
print(travel_log)