0

I just couldn't find an appropriate answers.

Consider - [A:B] and {A:B} . I think, [A:B] pairing is not valid for Python. Could you help me to figure it out?

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32

2 Answers2

2

In Python, Key-Value pair only exist in dictionary. You can only insert independent elements into lists that can be indexed by integers. You can't have a key-value pair in list.

I think, [A:B] pairing is not valid for Python. Could you help me to figure it out?

Yes, that is partly correct ! You can't have a key-value pair in list. They will exists only in dictionary. The {A:B} is correct where A is the key and B is the value which can be accessed using dict_name['A'] .

However, lst[A:B] can be considered as a slicing operation, where lst is any list. Slice operation is performed on Lists with the use of colon(:). Here, it will return you elements from index A to B. The syntax is - list_name[start : stop : steps] . Eg -

lst = [1, 2, 3, 5, 10]

print(lst[0:3])  # prints elements from index 0 to 3-1
>>> [1, 2, 3]

print(lst[:])    # prints all the elements in the list 
>>> [1, 2, 3, 5, 10]

print(lst[::-1]) # prints all the elements in the list in reverse order
>>> [10, 5, 3, 2, 1]

print(lst[0::2]) # prints elements from index 0 with a step of 2
>>> [1, 3, 10]

The best you can do is imitate the key-value pair of dictionary into list by inserting a tuple pair or you can store key value pairs of different dictionary data structures as elements in the list.

lst = [{1:3,4:12},{2:3,5:6,8:9}]

print( lst[1][2]) # prints element with key 2 of dictionary at index 1
>>> 3

print(lst[0][4])  # prints element with key 4 of dictionary at index 0
>>> 12

The above list stores two dictionaries in it as elements. You can index the dictionary and they use it to find any key-value pair that you want.


In short, use Dictionary whenever you need to store unordered associative data ( key - value pair) (They are insertion ordered in Python 3.6+. More info - here). Use Lists whenever you want to store an ordered collection of items.

Hope you understand the difference !

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • 2
    I totally agree that in general, you should think of dictionaries as unordered, but in Python 3.7 + they actually are guaranteed to maintain insertion order. But, for example, `{'a':1, 'b':2} == {'b':2, 'a':1}` so although they keep order, it isn't used for much internally other than how it is printed and the order that things will be iterated in. – juanpa.arrivillaga Jun 19 '20 at 20:26
  • 1
    @juanpa.arrivillaga Thanks for mentioning that. I have added a link which contains good info regarding the same – Abhishek Bhagate Jun 19 '20 at 20:31
1

In Python, key/value pair (a.k.a. Dictionary) is written in the following way:

>>> d = {'name':'John', 'age':21}
>>> d['name']
'John'
>>> d['age']
21

[A:B] is used to access a list/array as shown in the example below:

>>> a = ['a', 'b', 'c', 'd', 'e']

>>> a[:]     ## All items in the list
['a', 'b', 'c', 'd', 'e']

>>> a[2:]    ## All items beginning from index 2
['c', 'd', 'e']

>>> a[:4]    ## All items from index 0 to 3 (or 4-1) 
['a', 'b', 'c', 'd']

>>> a[2:4]   ## All items from index 2 to 3 (or 4-1)
['c', 'd']