+ 6

[Solved](Python)Is that a way to access an element in an array?

array={[1,2],[3,4]} Can i write a line to ask Python to print the element "2" directly?

8th Nov 2021, 2:12 AM
Lea
Lea - avatar
17 Respuestas
+ 8
Oh it looks like you have to define an array using [] instead of {}. Look at the following code https://code.sololearn.com/c3hrKJ23XPw6/?ref=app
8th Nov 2021, 3:24 AM
Rishi
Rishi - avatar
+ 8
Ah… I got it! I did some test and it seems it works for () and [], but never {}. Thank you very much for helping me understand! https://code.sololearn.com/cNr1Cdi8If1o/?ref=app
8th Nov 2021, 3:42 AM
Lea
Lea - avatar
+ 6
静文 毛 Sets are similar to lists or dictionaries. They are created using curly braces, and are unordered, which means that they can't be indexed. Due to the way they're stored, it's faster to check whether an item is part of a set using the in operator, rather than part of a list. a = {1,2,3,4} print(3 in a)
8th Nov 2021, 5:42 AM
MATOVU CALEB
MATOVU CALEB - avatar
+ 5
Lea a list makes it ordered because it can be indexed. And indexing starts from Zero Eg. a = [1,2,3,4] print(a[0]). Whereas. A set is unordered and can't be indexed. The difference is you can access elements in a list by their index while an element in a set is accessed using the in keyword
8th Nov 2021, 2:26 PM
MATOVU CALEB
MATOVU CALEB - avatar
+ 4
MATOVU CALEB Ok, understand, thank you for helping me!
8th Nov 2021, 2:35 PM
Lea
Lea - avatar
+ 4
Let me put this for future help and to anyone who comes across this. Use: () for tuples [] for lists {} for sets and dictionaries Whenever you want to index, make sure you build your arrays using lists. It helps a lot also whenever you want to change items in the list unlike tuples where you index but can't items in the tuple With dictionaries, you will index the keys in order to get the values. For sets, it's more useful in mathematics. Basically Real Analysis. In your case, a more elaborate answer would be, Array = [[1,2], [1,3]] #N/B this is now a 2D array in the case of numpy To print 2 directly. Note that the first element in the array is, [1,2] and the second element is [1,3]. Don't forget python is a zero indexing language. Thus, array[0][1] prints 2 as the output. [0] means the first item in the array list. And [1] means the second element in the first element of array list. I hope this clarifies further and adds more on top of the variety of feedbacks you have gotten.
8th Nov 2021, 9:47 PM
Walter
Walter - avatar
+ 3
You mean, other than the statement print(array[0][1]) ? I don't think there's another way without using this kind of indexing
8th Nov 2021, 2:42 AM
Rishi
Rishi - avatar
+ 3
array={[1,2],[3,4]} print(array[0][1]) The code above doesn’t return “2”, instead it returns a TypeError: unhashable type: ‘list’
8th Nov 2021, 2:49 AM
Lea
Lea - avatar
+ 3
Oladimeji Abdrahman MATOVU CALEB I checked the difference between list and set. It says: A set is unordered and unindexed, and dosen’t allow duplicates. A list is ordered. How do you understand “ordered”? I kinda get confuse what is ordered/unordered? It seems I can put various type of data in a list as well, i.e.: sample_list = [1, “red”, False, (0.5, 0.6, 0.7)] It doesn’t look very order to me….
8th Nov 2021, 2:11 PM
Lea
Lea - avatar
+ 3
Walter Thank you for helping!
8th Nov 2021, 11:36 PM
Lea
Lea - avatar
9th Nov 2021, 8:44 AM
✓`•°`™AnOnimus™`°•`✓ فايز الله‎
✓`•°`™AnOnimus™`°•`✓   فايز الله‎ - avatar
+ 2
That makes sense. Thank you Matovu!
8th Nov 2021, 5:54 AM
Lea
Lea - avatar
+ 1
Actually, as [] is for array data type, {} is for set data type, so using that means you're declaring an entire different data type although there's a way it can also be access
8th Nov 2021, 10:30 AM
Oladimeji Abdrahman
Oladimeji Abdrahman - avatar
+ 1
How to haking wife
9th Nov 2021, 7:51 AM
Irfan Ahmed
Irfan Ahmed - avatar
+ 1
Irfan Ahmed whose wife you wanna hack? I won't recommend you hacking anyone's wife, to preserve world peace =)
9th Nov 2021, 7:57 AM
Rishi
Rishi - avatar
0
静文 毛 happy to help =)
8th Nov 2021, 3:58 AM
Rishi
Rishi - avatar
- 5
8th Nov 2021, 11:54 AM
Олена Хміль